Python Pandas Write to Excel + Examples

In this Python Pandas tutorial, we will learn about Python Pandas Write DataFrame to Excel.

In Pandas, writing a DataFrame to an Excel file is a common task that can be accomplished in 3 ways. The most popular methods include:

  • Using to_excel() method
  • Using openpyxl library
  • Using xlsxwriter library

Python Pandas Write DataFrame to Excel

Here we will discuss 3 different methods to write a DataFrame to Excel with examples. Let us start with the first example.

Method-1: Using to_excel() method

The to_excel() method is a convenient and straightforward way to write a DataFrame to an Excel file. It is a built-in method of the Pandas DataFrame class and can be used by simply calling df.to_excel() where df is the DataFrame object.

import pandas as pd

# Creating a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Writing the dataframe to an Excel file
df.to_excel("dataframe.xlsx", index=False)

The above code uses the Pandas library to create a sample dataframe (a table-like data structure) and writes it to an Excel file.

  • The dataframe is created with three columns (A, B, and C) and three rows of data.
  • The to_excel method is then used to write the dataframe to an Excel file named “dataframe.xlsx”, with the index argument set to False to exclude the index from the file.
Python Pandas Write DataFrame to Excel using to_excel method
Python Pandas Write DataFrame to Excel using to_excel method

Read: Python Pandas DataFrame Iterrows

Method-2: Using openpyxl library

To write a DataFrame to an Excel file using the openpyxl library, we need to create a Pandas ExcelWriter object and call the to_excel() method on the DataFrame object.

import pandas as pd
import openpyxl

# Creating a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Writing the dataframe to an Excel file
writer = pd.ExcelWriter("dataframe.xlsx", engine='openpyxl')
df.to_excel(writer, index=False)
writer.save()

The above code creates a sample dataframe in Pandas and writes it to an Excel file using the openpyxl library.

  • The Pandas ExcelWriter class is used to create a writer object that can write the dataframe to the Excel file.
  • The dataframe is written to the writer object with the to_excel method and the index argument is set to False. The writer object is then saved to the Excel file with the save method.
Pandas Write DataFrame to Excel using excel_writer method
Pandas Write DataFrame to Excel using excel_writer method

Read: Pandas Delete Column

READ:  Django Upload Image File

Method-3: Using xlsxwriter library

In this method, the Pandas ExcelWriter class is then used to create a writer object that can write the dataframe to an Excel file.

import pandas as pd
import xlsxwriter

# Creating a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Writing the dataframe to an Excel file
writer = pd.ExcelWriter("dataframe.xlsx", engine='xlsxwriter')
df.to_excel(writer, index=False)
writer.save()

The above code uses the Pandas library and the xlsxwriter library to create a sample dataframe (a table-like data structure) and write it into an Excel file.

  • The dataframe is created with three columns (A, B, and C) and three rows of data.
  • The writer object is created with the name “dataframe.xlsx” and the engine is set to ‘xlsxwriter’ to specify the use of the xlsxwriter library.
  • The dataframe is then written to the writer object using the to_excel method, with the index argument set to False to exclude the index from the file.
  • Finally, the save method is called on the writer object to save the dataframe to the Excel file.
Python Pandas Write DataFrame to Excel using xlsxwriter
Python Pandas Write DataFrame to Excel using xlsxwriter

You may also like to read the following Python Pandas tutorials.

In this tutorial, we have learned about Python Pandas Write DataFrame to Excel by following the below methods:

  • Using to_excel() method
  • Using openpyxl library
  • Using xlsxwriter library