DataFrame.to_excel() method in Pandas
Last Updated :
18 Dec, 2025
DataFrame.to_excel() is a Pandas method used to export a DataFrame into an Excel file. It lets you save data to a specific file, choose the sheet name, include/exclude indexes, and write selected columns if needed. This method is helpful when you want to store processed data, share results, or create structured reports.
Example: This example shows how to export a DataFrame to an Excel file with the default settings, single sheet and index included.
Python
import pandas as pd
df = pd.DataFrame({"Name": ["A", "B"], "Score": [85, 92]})
df.to_excel("data.xlsx")
Output
OutputExplanation: df.to_excel("data.xlsx") saves the DataFrame to an Excel file named data.xlsx in the current directory.
Syntax
DataFrame.to_excel(excel_writer, sheet_name="Sheet1", **kwargs)
Parameters:
- excel_writer: File path or existing ExcelWriter object.
- sheet_name: Name of the sheet to write to. Default is "Sheet1".
- columns: Write only the selected columns.
- index: Whether to include row index. Default True.
- index_label: Label for index column(s).
Examples
Example 1: This example writes a DataFrame to an Excel file and customizes the sheet name.
Python
import pandas as pd
df = pd.DataFrame({"ID": [1, 2, 3], "Marks": [85, 91, 76]})
df.to_excel("students.xlsx", sheet_name="MarksData")
Output
Excel file created: students.xlsx with sheet MarksDataExplanation: sheet_name="MarksData" changes the sheet name from default "Sheet1".
Example 2: This example exports the DataFrame but removes the default numeric index from the Excel file.
Python
import pandas as pd
df = pd.DataFrame({"City": ["Delhi", "Mumbai", "Pune"], "Temp": [30, 32, 29]})
df.to_excel("city_temp.xlsx", index=False)
Output
Excel file created without index column.Explanation: index=False prevents the index column from being written to the file.
Example 3: This example shows how to save multiple DataFrames into different sheets of the same Excel file using ExcelWriter.
Python
import pandas as pd
df1 = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
df2 = pd.DataFrame({"X": [10, 20], "Y": [30, 40]})
with pd.ExcelWriter("multi_sheet.xlsx") as writer:
df1.to_excel(writer, sheet_name="SheetA")
df2.to_excel(writer, sheet_name="SheetB")
Output: Sheet A
Sheet ASheet B:
Sheet BExplanation:
- pd.ExcelWriter("multi_sheet.xlsx") allows writing multiple sheets into one file.
- df1.to_excel(writer, sheet_name="SheetA") writes the first sheet.
- df2.to_excel(writer, sheet_name="SheetB") writes the second sheet.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice