Export a Pandas DataFrame to Excel without the Index

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# Table of Contents

  1. Export a Pandas DataFrame to Excel without the Index
  2. Exporting the DataFrame to Excel without the index using pd.ExcelWriter

# Export a Pandas DataFrame to Excel without the Index

To export a Pandas DataFrame to Excel without the index:

  1. Use the DataFrame.to_excel() method to write the DataFrame object to an Excel sheet.
  2. Set the index argument to False when calling to_excel().
  3. When the index argument is False, the row names (index) won't be written to the file.
main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) df.to_excel('output.xlsx', sheet_name='Sheet1', index=False)

export pandas dataframe to excel without the index

The code for this article is available on GitHub

Running the code sample produces the following output.xlsx file in the same directory.

output xlsx contents without index

Notice that the file doesn't have a column with the indices of the DataFrame.

If you remove the index argument from the call to DataFrame.to_excel(), it defaults to True, which means that a column containing the indices is produced.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) df.to_excel('output.xlsx', sheet_name='Sheet1')

And here is the output.xlsx file.

output xlsx file with index

The index argument defaults to True.

The argument determines whether the row names (indices) are written to a column in the Excel file.

# Exporting the DataFrame to Excel without the index using pd.ExcelWriter

The same can be achieved if you use the pandas.ExcelWriter class.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) with pd.ExcelWriter('output.xlsx') as writer: df.to_excel(writer, sheet_name='Sheet1', index=False)

export pandas dataframe to excel without index using excel writer

output xlsx conents with excel writer

And here is the output.xlsx file.

The code for this article is available on GitHub

The pandas.ExcelWriter class can be used as a context manager.

The class enables us to write DataFrame objects into Excel sheets.

The only argument we passed to the class is the path to the xls, xlsx or ods file.

Make sure your call to df.to_excel() is indented correctly.

When using the pandas.ExcelWriter() class as a context manager, it automatically takes care of closing the Excel file after you are done writing.

However, you don't necessarily have to use the pandas.ExcelWriter class as a context manager.

Here is an example.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) writer = pd.ExcelWriter('output.xlsx') df.to_excel(writer, sheet_name='Sheet1', index=False) writer.save()

The writer.save() method closes the file object so you don't have any memory leaks.

The method should only be used after you are done writing to the file.

There is also an ExcelWriter.close method.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) writer = pd.ExcelWriter('output.xlsx') df.to_excel(writer, sheet_name='Sheet1', index=False) writer.close()
The code for this article is available on GitHub

The close method is a synonym for save.

When manually opening the Excel file for writing, make sure to close it, otherwise, you'd get a memory leak.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.