How to Export Pandas DataFrame to an Excel File

You can export Pandas DataFrame to an Excel file using to_excel.

Here is a template that you may apply in Python to export your DataFrame:

df.to_excel(r'Path where the exported excel file will be stored\File Name.xlsx', index = False)

And if you want to export your DataFrame to a specific Excel Sheet, then you may use this template:

df.to_excel(r'Path where the exported excel file will be stored\File Name.xlsx', sheet_name='Your sheet name', index = False)

Note: you’ll have to install openpyxl if you get the following error:

ModuleNotFoundError: No module named ‘openpyxl’

You may then use PIP to install openpyxl as follows:

pip install openpyxl

In the next section, you’ll see a simple example, where:

  • A DataFrame will be created from scratch
  • Then, the DataFrame will be exported to an Excel file

Example used to Export Pandas DataFrame to an Excel File

Let’s say that you have the following dataset about products and their prices:

Product Price
Desktop Computer 1200
Printer 150
Tablet 300
Monitor 450

The ultimate goal is to export that dataset into Excel.

But before you export that data, you’ll need to create a DataFrame in order to capture this information in Python.

You may then use the following syntax to create the DataFrame:

import pandas as pd

data = {'Product': ['Desktop Computer','Printer','Tablet','Monitor'],
        'Price': [1200,150,300,450]
        }

df = pd.DataFrame(data, columns = ['Product', 'Price'])

print (df)

This is how the DataFrame would look like:

            Product  Price
0  Desktop Computer   1200
1           Printer    150
2            Tablet    300
3           Monitor    450

Next, you’ll need to define the path where you’d like to store the exported Excel file.

For example, the path below will be used to store the exported Excel file (note that you’ll need to adjust the path to reflect the location where the Excel file will be stored on your computer):

r‘C:\Users\Ron\Desktop\export_dataframe.xlsx’

Notice that 3 components are highlighted in relation to that path:

  • In yellow, the ‘r’ character is placed before the path to avoid this unicode error: SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
  • In blue, the file name to be created is specified. You may type a different file name based on your needs
  • In green, the file type is specified. Since we are dealing with an Excel file, the file type is ‘.xlsx’ for the latest version of Excel

Putting everything together, here is the full Python code to export Pandas DataFrame to an Excel file:

import pandas as pd

data = {'Product': ['Desktop Computer','Printer','Tablet','Monitor'],
        'Price': [1200,150,300,450]
        }

df = pd.DataFrame(data, columns = ['Product', 'Price'])

df.to_excel (r'C:\Users\Ron\Desktop\export_dataframe.xlsx', index = False, header=True)

Finally, run the above code in Python (adjusted to your path), and you’ll notice that a new Excel file (called export_dataframe) would be created at the location that you specified.

Note that if you wish to include the index, then simply remove “, index = False” from your code.

Additional Resources

You just saw how to export Pandas DataFrame to an Excel file. At times, you may need to export Pandas DataFrame to a CSV file. The concept would be quite similar in such cases.

You may also want to check the Pandas Documentation for additional information about df.to_excel.