0

I develop data analysis app with Python. The data in the file is deleted when I add data to it again when I do df.to_excel. How can I add new data to the excel file while preserving other data Thanks,

sorry for my bad english

I used This code for add data

def ageGroup(city,age,period):
 ....
 df.to_excel('test.xlsx')
 ...

for i in range(len(city_list)):
for j in range(len(age_list_)):
    ageGroup(city_list[i], age_list_[j], 22)
jrmm
  • 43
  • 1
  • 5
  • The helper function in the first answer here will do everyhing you want and more. https://stackoverflow.com/questions/38074678/append-existing-excel-sheet-with-new-dataframe-using-python-pandas – Sid May 01 '19 at 12:04

1 Answers1

1

Have you looked at these parameters from the to_excel method ?

startrow : int, default 0
Upper left cell row to dump data frame.

startcol : int, default 0
Upper left cell column to dump data frame.

You could open your data, get the length of it, and write more starting at the length of your data +1.

Xhattam
  • 195
  • 1
  • 11
  • This is not happening either. I did startrow = 0 and I increased the startrow variable len (df) +1 in each loop. Didn't add under – jrmm May 01 '19 at 10:29