Issue
I have a huge data in text file consisting of details like Date, College name, Year of Passing also it contains Name and Unique id of students as shown below. Also the text file is not formatted and it contains lot of data.
Group list of all the students
5 June 2020/KCT/2015 Group BRD Rahul e34 Pradeep e44 Venkat r45 Azhar t54
6 June 2020/BCT/2012 Group ZRD Akash e14 Pavan e24 Vipul r15 Asad t14
7 June 2020/KBN/2014 Group KRD Fairoz e45 Kumar e55 Akshay e44 Vivek e99 etc
When i run a python code, I need the output in a excel/csv sheet where it displays only Name (Column1) and Unique id (Column2) row by row. Basically in the excel sheet i want only names and unique ids to be displayed in Excel sheet as shown below only i need names and unique id to be displayed. I do not need other data in the excel sheet.
Rahul e34
Pradeep e44
Venkat r45
Azhar t54
Akash e14
Pavan e24
Vipul r15
Asad t14
Fairoz e45
Kumar e55
Akshay e44
Vivek e99
This is what i have tried
import pandas as pd
df = pd.read_csv("C:\Users\PMishra\Desktop\Document.txt", sep='\t' )
df.to_csv('C:\Users\PMishra\Desktop\Demo.csv')
When i run this it will just copy all the contents from text file to excel sheet. I want output in a excel/csv sheet where it displays all Names (Column1) and Unique ids (Column2) row by row. I am new to python (Spyder). How to get only names and ids in column1 and column2 respectively?
Solution
Your first row must be the name of the columns, then you can show only the two columns with the name:
dfnew = df[["namecolum1","namecolum1"]]
dfnew.to_csv('C:\Users\PMishra\Desktop\Demo.csv')
Answered By - Dayannex
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.