0

Ok so I am trying to import a spreadsheet in excel and export it out after its been formatted. I already formatted it. The format is repositioning columns ok. So when i export it for whatever reason it adds a column A with an empty title that numbers each row. Please help fix my code so that it does not print this first column. Yes I have done research I tried using "df.drop(df.columns[-1], axis=1)", It doesn't work.

df = pd.read_excel('demo1.xls')
cols = df.columns.tolist()
cols = cols[-1:] + cols[:-1]
df = df[cols] 
df.drop(df.columns[-1], axis=1)
df.to_excel('demo.xlsx')

enter image description here

Charlie Clark
  • 18,477
  • 4
  • 49
  • 55

1 Answers1

1

The extra column you are seeing is most probably the index column. The to_excel method allows you to drop this column when exporting to Excel. See the documentation here

You want to do this to get rid of the index column:

...
df.to_excel('demo.xlsx', index=False)
Jan Morawiec
  • 425
  • 2
  • 11