Exporting a Pandas DataFrame to an Excel file
Last Updated :
07 Oct, 2024
Sometimes we need an Excel file for reporting, so as a coder we will see how to export Pandas DataFrame to an Excel file. The to_excel()
function in the Pandas library is utilized to export a DataFrame to an Excel sheet with the .xlsx extension.
Syntax
# saving the excel
dataframe_name.to_excel(file_name.xlsx)
Below are different examples by which we can export our Pandas DataFrame to an Excel File.
Pandas DataFrame to Excel Using to_excel() Function
In this example, a Pandas DataFrame named marks_data
is created to store information about students’ IDs, names, marks, and grades. The data is then saved to an Excel file named ‘MarksData.xlsx’ using the to_excel()
function.
Python
# importing the module
import pandas as pd
# creating the DataFrame
marks_data = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12,
3: 13, 4: 67, 5: 89,
6: 90, 7: 56, 8: 34},
'Name': {0: 'Ram', 1: 'Deep',
2: 'Yash', 3: 'Aman',
4: 'Arjun', 5: 'Aditya',
6: 'Divya', 7: 'Chalsea',
8: 'Akash'},
'Marks': {0: 89, 1: 97, 2: 45, 3: 78,
4: 56, 5: 76, 6: 100, 7: 87,
8: 81},
'Grade': {0: 'B', 1: 'A', 2: 'F', 3: 'C',
4: 'E', 5: 'C', 6: 'A', 7: 'B',
8: 'B'}})
# determining the name of the file
file_name = 'MarksData.xlsx'
# saving the excel
marks_data.to_excel(file_name)
print('DataFrame is written to Excel File successfully.')
Output:
DataFrame is written to Excel File successfully.
The Excel file is:

Export DataFrame to an Excel file Using ExcelWriter() Method
In this example, a Pandas DataFrame named cars_data
is created to store information about different car models, their maximum speeds, and colors. The data is then written to an Excel file named ‘CarsData1.xlsx’ using the to_excel()
function along with an ExcelWriter object, and the file is saved successfully.
Python
# importing the module
import pandas as pd
# creating the DataFrame
cars_data = pd.DataFrame({'Cars': ['BMW', 'Audi', 'Bugatti',
'Porsche', 'Volkswagen'],
'MaxSpeed': [220, 230, 240, 210, 190],
'Color': ['Black', 'Red', 'Blue',
'Violet', 'White']})
# writing to Excel
datatoexcel = pd.ExcelWriter('CarsData1.xlsx')
# write DataFrame to excel
cars_data.to_excel(datatoexcel)
# save the excel
datatoexcel.close()
print('DataFrame is written to Excel File successfully.')
Output:
DataFrame is written to Excel File successfully.
Excel File

Pandas Write to Excel using with Statemenet
In this example, a Pandas DataFrame named marks_data
is created to represent student information. The data is then written to an Excel file named ‘MarksData.xlsx’ with a sheet named ‘Employee’ using an ExcelWriter object, and a success message is printed.
Python
# importing the module
import pandas as pd
# creating the DataFrame
marks_data = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12,
3: 13, 4: 67, 5: 89,
6: 90, 7: 56, 8: 34},
'Name': {0: 'Ram', 1: 'Deep',
2: 'Yash', 3: 'Aman',
4: 'Arjun', 5: 'Aditya',
6: 'Divya', 7: 'Chalsea',
8: 'Akash'},
'Marks': {0: 89, 1: 97, 2: 45, 3: 78,
4: 56, 5: 76, 6: 100, 7: 87,
8: 81},
'Grade': {0: 'B', 1: 'A', 2: 'F', 3: 'C',
4: 'E', 5: 'C', 6: 'A', 7: 'B',
8: 'B'}})
# determining the name of the file
file_name = 'MarksData.xlsx'
# creating an ExcelWriter object
with pd.ExcelWriter(file_name) as writer:
# writing to the 'Employee' sheet
marks_data.to_excel(writer, sheet_name='Employee', index=False)
print('DataFrames are written to Excel File successfully.')
Output:
DataFrame is written to Excel File successfully.
Employee Excel Sheet Output

Exporting a Pandas DataFrame to an Excel file – FAQs
How do I write Pandas DataFrames to an existing Excel spreadsheet?
To write to an existing Excel spreadsheet without overwriting other data, you can use the ExcelWriter
object from Pandas along with the openpyxl
library.
Example:
from openpyxl import load_workbook
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({‘Data’: [50, 60, 70, 80]})
# Load existing workbook
book = load_workbook(‘output.xlsx’)
writer = pd.ExcelWriter(‘output.xlsx’, engine=’openpyxl’)
writer.book = book
# Write to a specific sheet
df.to_excel(writer, sheet_name=’Sheet1′, index=False, startrow=book[‘Sheet1’].max_row, header=False)
# Save the changes
writer.save()
Which is better, XlsxWriter or openpyxl?
Both XlsxWriter
and openpyxl
are excellent libraries for working with Excel files in Python, but they have different strengths:
- XlsxWriter: Offers faster performance and more advanced formatting and charting features. It’s better for writing data to new files.
- openpyxl: Can read and write Excel files, so it is suitable for modifying existing files. Offers more comprehensive support for reading Excel features like formulas, charts, and macros.
The choice between XlsxWriter and openpyxl often depends on the specific needs of your task (e.g., file modification versus file creation, need for advanced formatting).
How to export Pandas to Excel without index?
To export a DataFrame to Excel without including the DataFrame index, set the index
parameter to False
in the to_excel()
method.
Example:
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [24, 28]})
# Export to Excel without index
df.to_excel(‘no_index_output.xlsx’, index=False)
Similar Reads
Exporting a Pandas DataFrame to an Excel file
Sometimes we need an Excel file for reporting, so as a coder we will see how to export Pandas DataFrame to an Excel file. The to_excel() function in the Pandas library is utilized to export a DataFrame to an Excel sheet with the .xlsx extension. Syntax # saving the exceldataframe_name.to_excel(file_
4 min read
Export Pandas dataframe to a CSV file
Suppose you are working on a Data Science project and you tackle one of the most important tasks, i.e, Data Cleaning. After data cleaning, you don't want to lose your cleaned data frame, so you want to save your cleaned data frame as a CSV. Let us see how to export a Pandas DataFrame to a CSV file.
2 min read
Exporting Pandas DataFrame to JSON File
Let us see how to export a Pandas DataFrame as a JSON file. To perform this task we will be using the DataFrame.to_json() and the pandas.read_json() function. Example 1 : C/C++ Code # importing the module import pandas as pd # creating a DataFrame df = pd.DataFrame([['a', 'b', 'c'], ['d', 'e', 'f'],
2 min read
How to export Pandas DataFrame to a CSV file?
Let us see how to export a Pandas DataFrame to a CSV file. We will be using the to_csv() function to save a DataFrame as a CSV file. DataFrame.to_csv() Syntax : to_csv(parameters) Parameters : path_or_buf : File path or object, if None is provided the result is returned as a string. sep : String of
3 min read
How to export a DataFrame to Excel File in R ?
It is a frequent requirement to save our dataframe for portability after working on it on the auxiliary memory of the computer system using R Programming Language. In this article, we will be using writexl package to export our dataframe to excel(.xlsx). The write_xlsx() function of the writexl pack
3 min read
DataFrame.to_excel() method in Pandas
The to_excel() method is used to export the DataFrame to the excel file. To write a single object to the excel file, we have to specify the target file name. If we want to write to multiple sheets, we need to create an ExcelWriter object with target filename and also need to specify the sheet in the
2 min read
Export Dataframes to Multiple Excel Sheets in R
An excel workbook is used to contain tabular information stored within a cell-like structure. Every excel workbook in R contains multiple sheets to contain the data belonging to the same domain information. The excel sheet can contain columns belonging to different data types. The Excel files can be
5 min read
How to Append Pandas DataFrame to Existing CSV File?
In this discussion, we'll explore the process of appending a Pandas DataFrame to an existing CSV file using Python. Add Pandas DataFrame to an Existing CSV File. To achieve this, we can utilize the to_csv() function in Pandas with the 'a' parameter to write the DataFrame to the CSV file in append mo
3 min read
Adding New Column to Existing DataFrame in Pandas
Adding a new column to a DataFrame in Pandas is a simple and common operation when working with data in Python. You can quickly create new columns by directly assigning values to them. Let's discuss how to add new columns to the existing DataFrame in Pandas. There can be multiple methods, based on d
6 min read
Loading Excel spreadsheet as pandas DataFrame
Pandas is a very powerful and scalable tool for data analysis. It supports multiple file format as we might get the data in any format. Pandas also have support for excel file format. We first need to import Pandas and load excel file, and then parse excel file sheets as a Pandas dataframe. import p
1 min read