92

I'm trying to print out a dataframe from pandas into Excel. Here I am using to_excel() functions. However, I found that the 1st column in Excel is the "index",

0   6/6/2021 0:00   8/6/2021 0:00
1   4/10/2024 0:00  6/10/2024 0:00
2   4/14/2024 0:00  6/14/2024 0:00

Is there any ways to get rid of the first column?

smci
  • 29,564
  • 18
  • 109
  • 144
lsheng
  • 3,079
  • 8
  • 31
  • 42

3 Answers3

157

You need to set index=False in to_excel in order for it to not write the index column out, this semantic is followed in other Pandas IO tools, see http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html and http://pandas.pydata.org/pandas-docs/stable/io.html

EdChum
  • 339,461
  • 188
  • 752
  • 538
  • 2
    Note that `index=False` does not work if you have MultiIndex columns (as of Pandas 1.3.4, Oct 2021). `NotImplementedError: Writing to Excel with MultiIndex columns and no index ('index'=False) is not yet implemented.` – wisbucky Oct 27 '21 at 15:08
42

Example: index = False

import pandas as pd

writer = pd.ExcelWriter("dataframe.xlsx", engine='xlsxwriter')
dataframe.to_excel(writer,sheet_name = dataframe, index=False)
writer.save() 
Anurag Singh
  • 431
  • 4
  • 4
0

I did that and got the error message:

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed.

The code is as follows where 'test' is a dataframe with no column names

test = pd.DataFrame(biglist)
writer = pd.ExcelWriter("test.xlsx", engine='xlsxwriter')
test.to_excel(writer,sheet_name=test, index=False)
writer.save()
GTaylor
  • 21
  • 2
  • The error occurred for test.to_excel() – GTaylor May 20 '22 at 15:50
  • Am using Python 3.8.8 – GTaylor May 20 '22 at 15:53
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 20 '22 at 18:03