+1 vote
in Programming Languages by (8.6k points)

The Excel file created using the following DataFrame has the first column filled with index values of the DataFrame.

df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

Current output

Data

0 10

1 20

2 30

3 20

4 15

5 30

6 45

Expected output

Data

10

20

30

20

15

30

45

How to remove index column?

1 Answer

0 votes
by (16.9k points)

You need to add parameter 'index=False' to function to_excel() to remove index column.

Here is an example:

import pandas as pd

# Create a Pandas dataframe from the data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1', index=False)

# Close the Pandas Excel writer and output the Excel file.
writer.save()

...