Pandas How To Uncategorized How to save dataframe as Excel file

How to save dataframe as Excel file

In this post you learn how to save dataframe as Excel file in Pandas library of Python. In pandas, you can save a DataFrame as an Excel file using the to_excel method.

Example of saving as Excel

Here is an example:

import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'col1': [1, 2, 3, 4, 5], 'col2': [3, 4, 5, 6, 7]})

# save the dataframe as an Excel file
df.to_excel('data.xlsx', index=False)

In this example, data.xlsx is the name of the Excel file that will be created. The index parameter is set to False to exclude the index column from the file. By default, the engine used to write the file will be ‘openpyxl’, but you can specify a different engine using the engine parameter.
See also:
How to write to existing Excel file in Pandas
How to read multiple Excel sheets in Pandas

Tags:

2 thoughts on “How to save dataframe as Excel file”

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post