2

I am currently having issues exporting updated code in a csv, xlsx, or txt file. When run in a Sublime's text editor, the code displays properly, however once exported using pandas .to function, the data isn't exporting as when run in Sublime's text editor. The data simply exports as the original document.

Code used below:

import pandas as pd

df = pd.read_csv('test_data.csv')

print(df['Name']) # column 1, column 0 = index

df.to_csv('modified_data.txt', index = False)

I'm expecting once exported, the data to be a .txt file with only the names listed in the first column. The remaining columns however still show up in the txt. file. Same applies when I try to convert the document to a .csv or .xlsx.

Any suggestions would be appreciated.

Andrea Ebano
  • 563
  • 1
  • 4
  • 16
Williamct
  • 73
  • 6
  • And why do you expect it to NOT write all the columns to the txt file? If you think your argument index = false will do the trick, the argument index is not for that purpose. It just tells not to add an additional index column. It will output both Column 0 and Column 1 to the txt file. – Arif Eqbal Jul 05 '19 at 06:04
  • @ArifEqbal I am aware what index = False does. Thats why i added the comment in line 3 stating that column 0 = index. Apologies as it could have been removed to make no confusion. I am not so much concerned about column 0, the index, as I am unable to export any updates in the proper format. This includes column 0 as print(df['Index'], column 3 as print(df['Type 1'], etc. No matter the change, pandas isn't exporting the changes but rather the original file. – Williamct Jul 05 '19 at 06:10

2 Answers2

1

You can try something similar to this to write the required column to a text file.

import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
df1=df['Name']
df1.to_csv(r'C:\\Users\\nharidax\\Documents\\data.txt', header=None, index=False, sep=' ', mode='a')
  • thank you for the suggestion. I was able to get it to work using vonSchweetz method. Was wondering if you knew how I could define multiple changes as opposed to making all the changes in the .to parameter as mentioned in my comment below. Thank you. – Williamct Jul 05 '19 at 06:38
0

The remaining columns will show up since in your command:df.to_csv('modified_data.txt', index = False), you haven't specified that you only want one of the columns to be exported.

Doing
df.to_csv('modified_data.txt', index = False,columns= ColumnNameYouWantToExport) should work.

References: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html`

  • thank you! that fixed it. Now if I have a lot of changes? Will I have to make all of those changes in the .to parameter as above? Or is there a way to def all my changes? Apologies if a simple question, started learning Python last week. – Williamct Jul 05 '19 at 06:20
  • So, you can check the different parameters of the `.to_csv` function in the reference link in my answer. At some point however, if the number of changes is large, you could use a view https://stackoverflow.com/questions/41765230/pandas-what-is-a-view or create a new dataframe with the modifications. (View is strongly preferred.). Happy to help! If this answer or any other one solved your issue, please mark it as accepted – vonSchweetz Jul 05 '19 at 06:45