Sitemap
Zeeshan Ali

Zeeshan Ali

I am a Python developer and I am here to share what i have learnt so far. Hope my posts will help you in your python development journey. www.letmeflutter.com

Python Dataframe to Excel File Generation

1 min readJul 19, 2023

Export a Python DataFrame to an Excel file using the popular library called Pandas. Make sure you have Pandas installed before proceeding. The ‘openpyxl’ module, which is required to work with Excel files. If you don’t have them, you can install them using pip:

pip install pandas
pip install openpyxl

Here’s a step-by-step guide on how to do it:

Get Zeeshan Ali’s stories in your inbox

Join Medium for free to get updates from this writer.

Step 1: Import the necessary libraries

import pandas as pd

Step 2: Create a DataFrame (If you already have one, you can skip this step)

# Example DataFrame
data = {
'Name': ['John', 'Jane', 'Sara'],
'Age': [25, 30, 22],
'Occupation': ['Engineer', 'Doctor', 'Teacher']
}
df = pd.DataFrame(data)

Step 3: Export the DataFrame to Excel

# Specify the file name for the Excel file
file_name = 'dataframe_data.xlsx'
# Export the DataFrame to Excel
df.to_excel(file_name, index=False)
# Set index=False to avoid saving the index column or make it True
#if you want to add it

That’s it! Now you should have an Excel file named ‘dataframe_data.xlsx’ in your current working directory containing the data from your DataFrame.

Remember that the above code assumes you have already created a DataFrame named df. If you have a different DataFrame, you can use the same to_excel() method to export it to Excel with the specified file name.

--

--

Zeeshan Ali
Zeeshan Ali

Written by Zeeshan Ali

I am a Python developer and I am here to share what i have learnt so far. Hope my posts will help you in your python development journey. www.letmeflutter.com