How to Export Pandas DataFrame to an Excel File

In order to export pandas DataFrame to an Excel file you may use to_excel in Python.

Here is the general form that you may apply in Python to export your DataFrame:

 

df.to_excel(r'Path where you want to store the exported excel file\File Name.xlsx')

 

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

Example used to export pandas DataFrame to an Excel File

Let’s say that you have the following data-set about cars and their prices:

 

BrandPrice
Honda Civic32,000
Toyota Corolla35,000
Ford Focus37,000
Audi A445,000

 

The goal is to export that data-set into Excel.

But before you export that data, you’ll need to create the DataFrame which will capture the cars information in Python.

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

 

from pandas import DataFrame

Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [32000,35000,37000,45000]
        }

df = DataFrame(Cars, columns= ['Brand', 'Price'])

print (df)

 

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

In my case, I decided to store the exported Excel file on my desktop. I used this path (you’ll need to adjust that path to the location that you want to store the Excel file on your computer):

C:\Users\Doron E\Desktop\export_dataframe.xlsx

Notice that I highlighted two components in relation to that path:

  • In blue, I specified the file name to be created once you run the Python code. You may specify a different file name if you’d like
  • In green, I specified the file type. Since we are dealing with an Excel file, the file type is usually ‘.xlsx’ for more recent versions of Excel (for older versions of Excel, use the file type of ‘.xls’)

You may also put ‘r’ before the path name to avoid the following unicode error:

SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

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

 

from pandas import DataFrame

Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [32000,35000,37000,45000]
        }

df = DataFrame(Cars, columns= ['Brand', 'Price'])

export_excel = df.to_excel (r'C:\Users\Doron E\Desktop\export_dataframe.xlsx', index = None, header=True) #Don't forget to add '.xlsx' at the end of the path

print (df)

 

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

Once you open the Excel file, you should see the cars information that we reviewed at the beginning of this post:

 

How to Export Pandas DataFrame to an Excel File

Conclusion

We 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 DataFrame.to_excel.