Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Pandas ExcelWriter() creating extra column

Tags:

python

pandas

How to I stop Pandas to_excel() function creating an extra column with the indexes in? If I run the following:

import pandas as pd 

df = pd.read_excel('in.xlsx')
#do some stuff to the dataframe
writer = pd.ExcelWriter('out.xlsx')
df.to_excel(writer)
writer.save()

.. the newly created file (out.xlsx) has an additional column which I don't want. I just want the columns identified in df.columns outputting without the additional indexes column.

This is a small step in a larger process so i can't just manually delete the column. Also, i don't want to use any other Excel writing packages such as XlsxWriter

Many thanks!

like image 225
jacanterbury Avatar asked Oct 18 '17 17:10

jacanterbury


1 Answers

You need to set index property to false, like this:

df.to_excel(writer, index=False)

As decribed in pandas documentation: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html

like image 186
Paulius Avatar answered Nov 14 '22 14:11

Paulius