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
1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
I think I know what you mean, but I can only think of reading the data first, merging it, and then writing it
stackoverflow.com/questions/27847258/… Thank you for your help, Rain! I found an answer here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.