DaVinci DaVinci - 4 months ago 14
Python Question

0 index when generating excel xlsx file using pandas

df1=pd.read_csv('out.csv')
df2=pd.read_excel('somefile.xls')

#put columns from df1 to df2

df2['eer']=df1['eer']
df2['wer']=df1['wer']
df2['zer']=df1['zer']
df2['qer']=df1['qer']
df2['der']=df1['der']

#make a new file with somefile.xls columns + the ones above

newfile = input("Enter a name for the combined file: ")
writer = pd.ExcelWriter(newfile)
df2.to_excel(writer)
writer.save()


I have this part of the code at the end of my program using python pandas module, so whenever I generate the new file with all the columns i need, column A has some kind of index going from 0 to the end row, instead of 'Name' which is what somefile's column A is. 'Name' is now column B.

like this

0
1
2
3
4
5
6
7
8
9

Answer

This question is a dupe of many many others, but all you would do is change

df2.to_excel(writer)

to

df2.to_excel(writer, index=False)