0

I am appending a DF to an excel sheet. For some reason my DF values are being appended to the last row of the first column in my excel, but I need it to be appended as the last column of my excel (which is column BK). I need to include the header as well but don't need the index.

The code I am using to append one specific column in my DF to my excel file

df = pd.DataFrame({"Specific_Column": ['', 'cell 5', 'cell 6']})

df.to_csv('excelfile.csv',columns=['Specific_Column'], mode='a',index=False)

What I am trying to achieve:

columnA in excel columnB in excel Specific_Column
Cell 1 Cell 2 Cell 5
Cell 3 Cell 4 Cell 6

What is happening:

columnA in excel columnB in excel
Cell 1 Cell 2
Cell 3 Cell 4
Specific_Column
---------------
Cell 5
Cell 6
bananas
  • 133
  • 8
  • This post answered my question: https://stackoverflow.com/questions/27847258/append-pandas-dataframe-column-to-csv – bananas Nov 01 '22 at 03:43

1 Answers1

0
df.to_csv('excelfile.csv',columns=['Excel Column A','Excel Column B','Specific_Column'], mode='a',index=False)

I can get the following results according to this code

enter image description here

enter image description here

Try this

Rain
  • 5
  • 2
  • Hi Rain, i made an update to my ask - column A and column B are columns within my excel and i only have 1 column in my df. I am trying to add it to the end of my data in my excel file. – bananas Oct 31 '22 at 13:14
  • I think I know what you mean, but I can only think of reading the data first, merging it, and then writing it – Rain Nov 01 '22 at 01:04
  • https://stackoverflow.com/questions/27847258/append-pandas-dataframe-column-to-csv Thank you for your help, Rain! I found an answer here – bananas Nov 01 '22 at 03:44