Python pandas write to excel

In this tutorial, you’ll learn how to save your Pandas DataFrame or DataFrames to Excel files. Being able to save data to this ubiquitous data format is an important skill in many organizations. In this tutorial, you’ll learn how to save a simple DataFrame to Excel, but also how to customize your options to create the report you want!

By the end of this tutorial, you’ll have learned:

  • How to save a Pandas DataFrame to Excel
  • How to customize the sheet name of your DataFrame in Excel
  • How to customize the index and column names when writing to Excel
  • How to write multiple DataFrames to Excel in Pandas
  • Whether to merge cells or freeze panes when writing to Excel in Pandas
  • How to format missing values and infinity values when writing Pandas to Excel

Let’s get started!

The Quick Answer: Use Pandas to_excel

To write a Pandas DataFrame to an Excel file, you can apply the .to_excel() method to the DataFrame, as shown below:

# Saving a Pandas DataFrame to an Excel File
# Without a Sheet Name
df.to_excel(file_name)

# With a Sheet Name
df.to_excel(file_name, sheet_name='My Sheet')

# Without an Index
df.to_excel(file_name, index=False)

Understanding the Pandas to_excel Function

Before diving into any specifics, let’s take a look at the different parameters that the method offers. The method provides a ton of different options, allowing you to customize the output of your DataFrame in many different ways. Let’s take a look:

# The many parameters of the .to_excel() function
df.to_excel(excel_writer, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None, storage_options=None)

Let’s break down what each of these parameters does:

Parameter Description Available Options
excel_writer= The path of the ExcelWriter to use path-like, file-like, or ExcelWriter object
sheet_name= The name of the sheet to use String representing name, default ‘Sheet1’
na_rep= How to represent missing data String, default ''
float_format= Allows you to pass in a format string to format floating point values String
columns= The columns to use when writing to the file List of strings. If blank, all will be written
header= Accepts either a boolean or a list of values. If a boolean, will either include the header or not. If a list of values is provided, aliases will be used for the column names. Boolean or list of values
index= Whether to include an index column or not. Boolean
index_label= Column labels to use for the index. String or list of strings.
startrow= The upper left cell to start the DataFrame on. Integer, default 0
startcol= The upper left column to start the DataFrame on Integer, default 0
engine= The engine to use to write. openpyxl or xlsxwriter
merge_cells= Whether to write multi-index cells or hierarchical rows as merged cells Boolean, default True
encoding= The encoding of the resulting file. String
inf_rep= How to represent infinity values (as Excel doesn’t have a representation) String, default 'inf'
verbose= Whether to display more information in the error logs. Boolean, default True
freeze_panes= Allows you to pass in a tuple of the row, column to start freezing panes on Tuple of integers with length 2
storage_options= Extra options that allow you to save to a particular storage connection Dictionary
The many parameters of the Pandas .to_excel() method

How to Save a Pandas DataFrame to Excel

The easiest way to save a Pandas DataFrame to an Excel file is by passing a path to the .to_excel() method. This will save the DataFrame to an Excel file at that path, overwriting an Excel file if it exists already.

Let’s take a look at how this works:

# Saving a Pandas DataFrame to an Excel File
import pandas as pd
df = pd.DataFrame.from_dict(
    {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
)

df.to_excel('filename.xlsx')

Running the code as shown above will save the file with all other default parameters. This returns the following image:

01 - Saving Pandas DataFrame to Excel with Index

You can specify a sheetname by using the sheet_name= parameter. By default, Pandas will use 'sheet1'.

# Specifying a Sheet Name When Saving to Excel
import pandas as pd
df = pd.DataFrame.from_dict(
    {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
)

df.to_excel('filename.xlsx', sheet_name='Your Sheet')

This returns the following workbook:

02 - Saving Pandas DataFrame to Excel with Sheet Name

In the following section, you’ll learn how to customize whether to include an index column or not.

How to Include an Index when Saving a Pandas DataFrame to Excel

By default, Pandas will include the index when saving a Pandas Dataframe to an Excel file. This can be helpful when the index is a meaningful index (such as a date and time). However, in many cases, the index will simply represent the values from 0 through to the end of the records.

If you don’t want to include the index in your Excel file, you can use the index= parameter, as shown below:

# How to exclude the index when saving a DataFrame to Excel
import pandas as pd
df = pd.DataFrame.from_dict(
    {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
)

df.to_excel('filename.xlsx', index=False)

This returns the following Excel file:

03- Saving Pandas DataFrame to Excel without Index

In the following section, you’ll learn how to rename an index when saving a Pandas DataFrame to an Excel file.

How to Rename an Index when Saving a Pandas DataFrame to Excel

By default, Pandas will not named the index of your DataFrame. This, however, can be confusing and can lead to poorer results when trying to manipulate the data in Excel, either by filtering or by pivoting the data. Because of this, it can be helpful to provide a name or names for your indices.

Pandas makes this easy by using the index_label= parameter. This parameter accepts either a single string (for a single index) or a list of strings (for a multi-index). Check out below how you can use this parameter:

# Providing a name for your Pandas index
import pandas as pd
df = pd.DataFrame.from_dict(
    {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
)

df.to_excel('filename.xlsx', index_label='Your Index')

This returns the following sheet:

04 - Saving Pandas DataFrame to Excel with Labelled Index

How to Save Multiple DataFrames to Different Sheets in Excel

One of the tasks you may encounter quite frequently is the need to save multi Pandas DataFrames to the same Excel file, but in different sheets. This is where Pandas makes it a less intuitive. If you were to simply write the following code, the second command would overwrite the first command:

# The wrong way to save multiple DataFrames to the same workbook
import pandas as pd
df = pd.DataFrame.from_dict(
    {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
)

df.to_excel('filename.xlsx', sheet_name='Sheet1')
df.to_excel('filename.xlsx', sheet_name='Sheet2')

Instead, we need to use a Pandas Excel Writer to manage opening and saving our workbook. This can be done easily by using a context manager, as shown below:

# The Correct Way to Save Multiple DataFrames to the Same Workbook
import pandas as pd
df = pd.DataFrame.from_dict(
    {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
)

with pd.ExcelWriter('filename.xlsx') as writer:
    df.to_excel(writer, sheet_name='Sheet1')
    df.to_excel(writer, sheet_name='Sheet2')

This will create multiple sheets in the same workbook. The sheets will be created in the same order as you specify them in the command above.

This returns the following workbook:

05 - Saving Multiple Pandas Dataframes to Excel Worksheets

How to Save Only Some Columns when Exporting Pandas DataFrames to Excel

When saving a Pandas DataFrame to an Excel file, you may not always want to save every single column. In many cases, the Excel file will be used for reporting and it may be redundant to save every column. Because of this, you can use the columns= parameter to accomplish this.

Let’s see how we can save only a number of columns from our dataset:

# Saving Only a Subset of Columns to Excel
import pandas as pd
df = pd.DataFrame.from_dict(
    {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
)

df.to_excel('filename.xlsx', columns=['A', 'B'])

This returns the following Excel file:

06 - Saving Pandas DataFrame to Excel with only some columns

How to Rename Columns when Exporting Pandas DataFrames to Excel

Continuing our discussion about how to handle Pandas DataFrame columns when exporting to Excel, we can also rename our columns in the saved Excel file. The benefit of this is that we can work with aliases in Pandas, which may be easier to write, but then output presentation-ready column names when saving to Excel.

We can accomplish this using the header= parameter. The parameter accepts either a boolean value of a list of values. If a boolean value is passed, you can decide whether to include or a header or not. When a list of strings is provided, then you can modify the column names in the resulting Excel file, as shown below:

# Modifying Column Names when Exporting a Pandas DataFrame to Excel
import pandas as pd
df = pd.DataFrame.from_dict(
    {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
)

df.to_excel('filename.xlsx', header=['New_A', 'New_B', 'New_C'])

This returns the following Excel sheet:

07 - Saving Pandas DataFrame to Excel with renamed columns.png

How to Specify Starting Positions when Exporting a Pandas DataFrame to Excel

One of the interesting features that Pandas provides is the ability to modify the starting position of where your DataFrame will be saved on the Excel sheet. This can be helpful if you know you’ll be including different rows above your data or a logo of your company.

Let’s see how we can use the startrow= and startcol= parameters to modify this:

# Changing the Start Row and Column When Saving a DataFrame to an Excel File
import pandas as pd
df = pd.DataFrame.from_dict(
    {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
)

df.to_excel('filename.xlsx', startcol=3, startrow=2)

This returns the following worksheet:

08 - Specifying starting position for excel

How to Represent Missing and Infinity Values When Saving Pandas DataFrame to Excel

In this section, you’ll learn how to represent missing data and infinity values when saving a Pandas DataFrame to Excel. Because Excel doesn’t have a way to represent infinity, Pandas will default to the string 'inf' to represent any values of infinity.

In order to modify these behaviors, we can use the na_rep= and inf_rep= parameters to modify the missing and infinity values respectively. Let’s see how we can do this by adding some of these values to our DataFrame:

# Customizing Output of Missing and Infinity Values When Saving to Excel
import pandas as pd
import numpy as np

df = pd.DataFrame.from_dict(
    {'A': [1, np.NaN, 3], 'B': [4, 5, np.inf], 'C': [7, 8, 9]}
)

df.to_excel('filename.xlsx', na_rep='NA', inf_rep='INFINITY')

This returns the following worksheet:

09 - Saving Pandas DataFrame to Excel with infinity and missing data

How to Merge Cells when Writing Multi-Index DataFrames to Excel

In this section, you’ll learn how to modify the behavior of multi-index DataFrames when saved to Excel. By default Pandas will set the merge_cells= parameter to True, meaning that the cells will be merged. Let’s see what happens when we set this behavior to False, indicating that the cells should not be merged:

# Modifying Merge Cell Behavior for Multi-Index DataFrames
import pandas as pd
import numpy as np
from random import choice

df = pd.DataFrame.from_dict({
    'A': np.random.randint(0, 10, size=50),
    'B': [choice(['a', 'b', 'c']) for i in range(50)],
    'C': np.random.randint(0, 3, size=50)})

pivot = df.pivot_table(index=['B', 'C'], values='A')

pivot.to_excel('filename.xlsx', merge_cells=False)

This returns the Excel worksheet below:

10 - Prevent merging cells when saving Pandas to Excel

How to Freeze Panes when Saving a Pandas DataFrame to Excel

In this final section, you’ll learn how to freeze panes in your resulting Excel worksheet. This allows you to specify the row and column at which you want Excel to freeze the panes. This can be done using the freeze_panes= parameter. The parameter accepts a tuple of integers (of length 2). The tuple represents the bottommost row and the rightmost column that is to be frozen.

Let’s see how we can use the freeze_panes= parameter to freeze our panes in Excel:

# Freezing Panes in an Excel Workbook Using Pandas
import pandas as pd
import numpy as np

df = pd.DataFrame.from_dict(
    {'A': [1, np.NaN, 3], 'B': [4, 5, np.inf], 'C': [7, 8, 9]}
)

df.to_excel('filename.xlsx', freeze_panes=(3,4))

This returns the following workbook:

11 - Saving Pandas DataFrame to Excel with frozen panes

Conclusion

In this tutorial, you learned how to save a Pandas DataFrame to an Excel file using the to_excel method. You first explored all of the different parameters that the function had to offer at a high level. Following that, you learned how to use these parameters to gain control over how the resulting Excel file should be saved. For example, you learned how to specify sheet names, index names, and whether to include the index or not. Then you learned how to include only some columns in the resulting file and how to rename the columns of your DataFrame. You also learned how to modify the starting position of the data and how to freeze panes.

Additional Resources

To learn more about related topics, check out the tutorials below:

  • How to Use Pandas to Read Excel Files in Python
  • Pandas Dataframe to CSV File – Export Using .to_csv()
  • Introduction to Pandas for Data Science
  • Official Documentation: Pandas to_excel

Время чтения 4 мин.

Python Pandas — это библиотека для анализа данных. Она может читать, фильтровать и переупорядочивать небольшие и большие наборы данных и выводить их в различных форматах, включая Excel. ExcelWriter() определен в библиотеке Pandas.

Содержание

  1. Что такое функция Pandas.ExcelWriter() в Python?
  2. Синтаксис
  3. Параметры
  4. Возвращаемое значение
  5. Пример программы с Pandas ExcelWriter()
  6. Что такое функция Pandas DataFrame to_excel()?
  7. Запись нескольких DataFrames на несколько листов
  8. Заключение

Метод Pandas.ExcelWriter() — это класс для записи объектов DataFrame в файлы Excel в Python. ExcelWriter() можно использовать для записи текста, чисел, строк, формул. Он также может работать на нескольких листах.Для данного примера необходимо, чтоб вы установили на свой компьютер библиотеки Numpy и Pandas.

Синтаксис

pandas.ExcelWriter(path, engine= None, date_format=None, datetime_format=None, mode=w,**engine_krawgs)

Параметры

Все параметры установлены на значения по умолчанию.

Функция Pandas.ExcelWriter() имеет пять параметров.

  1. path: имеет строковый тип, указывающий путь к файлу xls или xlsx.
  2. engine: он также имеет строковый тип и является необязательным. Это движок для написания.
  3. date_format: также имеет строковый тип и имеет значение по умолчанию None. Он форматирует строку для дат, записанных в файлы Excel.
  4. datetime_format: также имеет строковый тип и имеет значение по умолчанию None. Он форматирует строку для объектов даты и времени, записанных в файлы Excel.
  5. Mode: это режим файла для записи или добавления. Его значение по умолчанию — запись, то есть ‘w’.

Возвращаемое значение

Он экспортирует данные в файл Excel.

Пример программы с Pandas ExcelWriter()

Вам необходимо установить и импортировать модуль xlsxwriter. Если вы используете блокнот Jupyter, он вам не понадобится; в противном случае вы должны установить его.

Напишем программу, показывающую работу ExcelWriter() в Python.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

import pandas as pd

import numpy as np

import xlsxwriter

# Creating dataset using dictionary

data_set = {

    ‘Name’: [‘Rohit’, ‘Arun’, ‘Sohit’, ‘Arun’, ‘Shubh’],

    ‘Roll no’: [’01’, ’02’, ’03’, ’04’, np.nan],

    ‘maths’: [’93’, ’63’, np.nan, ’94’, ’83’],

    ‘science’: [’88’, np.nan, ’66’, ’94’, np.nan],

    ‘english’: [’93’, ’74’, ’84’, ’92’, ’87’]}

# Converting into dataframe

df = pd.DataFrame(data_set)

# Writing the data into the excel sheet

writer_obj = pd.ExcelWriter(‘Write.xlsx’,

                            engine=‘xlsxwriter’)

df.to_excel(writer_obj, sheet_name=‘Sheet’)

writer_obj.save()

print(‘Please check out the Write.xlsx file.’)

Выход:

Please check out the Write.xlsx file.

Содержимое файла Excel следующее.

Метод ExcelWriter()

В приведенном выше коде мы создали DataFrame, в котором хранятся данные студентов. Затем мы создали объект для записи данных DataFrame на лист Excel, и после записи данных мы сохранили лист. Некоторые значения в приведенном выше листе Excel пусты, потому что в DataFrame эти значения — np.nan. Чтобы проверить данные DataFrame, проверьте лист Excel.

Что такое функция Pandas DataFrame to_excel()?

Функция Pandas DataFrame to_excel() записывает объект на лист Excel. Мы использовали функцию to_excel() в приведенном выше примере, потому что метод ExcelWriter() возвращает объект записи, а затем мы используем метод DataFrame.to_excel() для его экспорта в файл Excel.

Чтобы записать один объект в файл Excel .xlsx, необходимо только указать имя целевого файла. Для записи на несколько листов необходимо создать объект ExcelWriter с именем целевого файла и указать лист в файле для записи.

На несколько листов можно записать, указав уникальное имя листа. При записи всех данных в файл необходимо сохранить изменения. Обратите внимание, что создание объекта ExcelWriter с уже существующим именем файла приведет к удалению содержимого существующего файла.

Мы также можем написать приведенный выше пример, используя Python с оператором.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

import pandas as pd

import numpy as np

import xlsxwriter

# Creating dataset using dictionary

data_set = {

    ‘Name’: [‘Rohit’, ‘Arun’, ‘Sohit’, ‘Arun’, ‘Shubh’],

    ‘Roll no’: [’01’, ’02’, ’03’, ’04’, np.nan],

    ‘maths’: [’93’, ’63’, np.nan, ’94’, ’83’],

    ‘science’: [’88’, np.nan, ’66’, ’94’, np.nan],

    ‘english’: [’93’, ’74’, ’84’, ’92’, ’87’]}

# Converting into dataframe

df = pd.DataFrame(data_set)

with pd.ExcelWriter(‘WriteWith.xlsx’, engine=‘xlsxwriter’) as writer:

    df.to_excel(writer, sheet_name=‘Sheet’)

print(‘Please check out the WriteWith.xlsx file.’)

Выход:

Please check out the WriteWith.xlsx file.

Вы можете проверить файл WriteWith.xlsx и просмотреть его содержимое. Это будет то же самое, что и файл Write.xlsx.

Запись нескольких DataFrames на несколько листов

В приведенном выше примере мы видели только один лист для одного фрейма данных. Мы можем написать несколько фреймов с несколькими листами, используя Pandas.ExcelWriter.

Давайте напишем пример, в котором мы создадим три DataFrames и сохраним эти DataFrames в файле multiplesheet.xlsx с тремя разными листами.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

import pandas as pd

import numpy as np

import xlsxwriter

# Creating dataset using dictionary

data_set = {

    ‘Name’: [‘Rohit’, ‘Arun’, ‘Sohit’, ‘Arun’, ‘Shubh’],

    ‘Roll no’: [’01’, ’02’, ’03’, ’04’, np.nan],

    ‘maths’: [’93’, ’63’, np.nan, ’94’, ’83’],

    ‘science’: [’88’, np.nan, ’66’, ’94’, np.nan],

    ‘english’: [’93’, ’74’, ’84’, ’92’, ’87’]}

data_set2 = {

    ‘Name’: [‘Ankit’, ‘Krunal’, ‘Rushabh’, ‘Dhaval’, ‘Nehal’],

    ‘Roll no’: [’01’, ’02’, ’03’, ’04’, np.nan],

    ‘maths’: [’93’, ’63’, np.nan, ’94’, ’83’],

    ‘science’: [’88’, np.nan, ’66’, ’94’, np.nan],

    ‘english’: [’93’, ’74’, ’84’, ’92’, ’87’]}

data_set3 = {

    ‘Name’: [‘Millie’, ‘Jane’, ‘Michael’, ‘Bobby’, ‘Brown’],

    ‘Roll no’: [’01’, ’02’, ’03’, ’04’, np.nan],

    ‘maths’: [’93’, ’63’, np.nan, ’94’, ’83’],

    ‘science’: [’88’, np.nan, ’66’, ’94’, np.nan],

    ‘english’: [’93’, ’74’, ’84’, ’92’, ’87’]}

# Converting into dataframe

df = pd.DataFrame(data_set)

df2 = pd.DataFrame(data_set2)

df3 = pd.DataFrame(data_set3)

with pd.ExcelWriter(‘multiplesheet.xlsx’, engine=‘xlsxwriter’) as writer:

    df.to_excel(writer, sheet_name=‘Sheet’)

    df2.to_excel(writer, sheet_name=‘Sheet2’)

    df3.to_excel(writer, sheet_name=‘Sheet3’)

print(‘Please check out the multiplesheet.xlsx file.’)

Выход:

Запись нескольких DataFrames на несколько листов

Вы можете видеть, что есть три листа, и каждый лист имеет разные столбцы имени.

Функция to_excel() принимает имя листа в качестве параметра, и здесь мы можем передать три разных имени листа, и этот DataFrame сохраняется на соответствующих листах.

Заключение

Как использовать метод Pandas.ExcelWriter в Python

Если вы хотите экспортировать Pandas DataFrame в файлы Excel, вам нужен только класс ExcelWriter(). Класс ExcelWrite() предоставляет объект записи, а затем мы можем использовать функцию to_excel() для экспорта DataFrame в файл Excel.

If you’re anything like me, you love finding new and interesting ways to work with data. And what could be more interesting than working with data in Excel?

Excel is a powerful tool for working with data, and Python Pandas makes it even easier. In this post, I’ll show you how to use Python Pandas to write data into an Excel file.

I’ll start by showing you how to install Pandas and create a DataFrame. Then I’ll show you how to write data to an Excel file using Pandas.

Also read: The xlrd Module – How To Handle Excel Files In Python?

What is an Excel file?

An Excel file is a spreadsheet file containing some cells in rows and columns (Tabular view) and can help in the arrangement, calculation, sorting, and managing of data.

The data in the spreadsheet may be numeric, text, formulas, hyperlinks, functions, etc. An XLS file stores data as binary streams. It can only be created by the most popular MS Excel or other spreadsheet programs. The file format .xlsx always indicates an excel file on its own.

The following image depicts an excel file created by the MS-excel program:

Excel File By MS Excel 1

The Excel File By MS Excel 1

The pandas Module

It is an open-source library or module in Python providing some in-built data structures and data analysis tools. Some of the key features of the pandas Module are as follows.

  • It provides efficient and faster ways of managing data and accessing it.
  • It has the capability to handle the missing data integrated into its library.
  • It has some unique techniques for indexing its data.
  • It offers a large variety of tools that are in-built, which help us to read and write data. 
  • It can support JSON, CSV, HDF5, and Excel files.
  • It is extremely responsible for merging various datasets.
  • It gives us access to other helpful libraries like MatPlotLib and NumPy.
  • It allows its users to implement all kinds of mathematical operations on the data sets.

Installing Python Pandas

Before getting started with this article, make sure you have installed this module on your computer. You can download this module by the command below on your command prompt.

You can get your pandas module installed as follows.

Installing Panda Module 2

Installing Panda Module 2

We are going to use one another module in this article, Let’s understand that as well.

Installing the openpyxl Module

Openpyxl is a Python library or module used to read or write from an Excel file. This module needs to be installed to use certain methods like load_workbook(), otherwise, we can’t use those methods, It will throw error. Let’s install this module using our command prompt.

C:Userspc> pip install openpyxl

The above code snippet will install our openpyxl module.

Installing Openpyxl Module 1

Installing Openpyxl Module

Writing a single spreadsheet to Excel file

We will first convert it into a Pandas Dataframe and then write the Dataframe to Excel. To export a Pandas DataFrame as an Excel file (i.e. .xlsx, .xls file), We will use the  to_excel() method.

Follow the below code snippet:

>>> import pandas as pd
>>> import openpyxl

>>> df = pd.DataFrame([['Hi', 'Hello', 'Hey'], ['Nandini', 'Rupali', 'Malina'], ['Ram','Shyam', 'Babu']], index=['one', 'two', 'three'], columns=['a', 'b', 'c'])

If we print the above data frame, We can get the result as follows.

>>> print(df)
             a       b       c
one         Hi   Hello     Hey
two    Nandini  Rupali  Malina
three      Ram   Shyam    Babu
>>>

Now We will export the above data frame to an excel file using the below code snippet.

df.to_excel('new_excel.xlsx', sheet_name='new_sheet_name')

The above code snippet will create a .xlsx file in our PC folder automatically.

Writing multiple Spreadsheet to Excel file

Let us create another data frame.

df2 = pd.DataFrame([[11, 21, 31], [12, 22, 32], [31, 32, 33]], index=['one', 'two', 'three'], columns=['a', 'b', 'c'])

Now we will export both df and df2 data frames into our new_excel file. follow the code snippet below

with pd.ExcelWriter('new_excel.xlsx') as writer:
    df.to_excel(writer, sheet_name='sheet1')
    df2.to_excel(writer, sheet_name='sheet2')

It will create an excel file comprising two slides as below.

Screenshot 448 1
sheet 1

Screenshot 449 1

sheet 2

Summary

In this article, we covered 2 examples of writing data into our Excel file using Python. We discussed two popular modules for our appropriate output. Hope you enjoyed this quick demontration.

Use pandas to_excel() function to write a DataFrame to an excel sheet with extension .xlsx. By default it writes a single DataFrame to an excel file, you can also write multiple sheets by using an ExcelWriter object with a target file name, and sheet name to write to.

Note that creating an ExcelWriter object with a file name that already exists will result in the contents of the existing file being erased.

Related: pandas read Excel Sheet

pandas to Excel key Points

  • By default, it uses xlsxwriter if it is installed otherwise it uses openpyxl
  • Supports saving multiple DataFrames to single sheet.
  • Save multiple sheets, append existing sheet or file.
  • Use ExcelWriter()

Let’s create a pandas DataFrame from list and explore usingto_excel() function by using multiple parameters.


import pandas as pd
import numpy as np

# Create multiple lists
technologies =  ['Spark','Pandas','Java','Python', 'PHP']
fee = [25000,20000,15000,15000,18000]
duration = ['5o Days','35 Days',np.nan,'30 Days', '30 Days']
discount = [2000,1000,800,500,800]
columns=['Courses','Fee','Duration','Discount']

# Create DataFrame from multiple lists
df = pd.DataFrame(list(zip(technologies,fee,duration,discount)), columns=columns)
print(df)

# Outputs
#  Courses    Fee Duration  Discount
#0   Spark  25000  5o Days      2000
#1  Pandas  20000  35 Days      1000
#2    Java  15000      NaN       800
#3  Python  15000  30 Days       500
#4     PHP  18000  30 Days       800

1. pandas DataFrame to Excel

Use to_excel() function to write or export pandas DataFrame to excel sheet with extension xslx. Using this you can write excel files to the local file system, S3 e.t.c. Not specifying any parameter it default writes to a single sheet.

to_excel() takes several optional params that can be used skip columns, skip rows, not to write index, set column names, formatting, and many more.


# Write DataFrame to Excel file
df.to_excel('Courses.xlsx')

This creates an excel file with the contents as below. By default, It exports column names, indexes, and data to a sheet named 'Sheet1'.

You can change the name of the sheet from Sheet1 to something that makes sense to your data by using sheet_name param. The below example exports it to the sheet named ‘Technologies‘.


# Write DataFrame to Excel file with sheet name
df.to_excel('Courses.xlsx', sheet_name='Technologies')

2. Write to Multiple Sheets

The ExcelWriter class allows you to write or export multiple pandas DataFrames to separate sheets. First, you need to create an object for ExcelWriter.

The below example writes data from df object to a sheet named Technologies and df2 object to a sheet named Schedule.


# Write to Multiple Sheets
with pd.ExcelWriter('Courses.xlsx') as writer:
    df.to_excel(writer, sheet_name='Technologies')
    df2.to_excel(writer, sheet_name='Schedule')

3. Append to Existing Excel File

ExcelWriter can be used to append DataFrame to an excel file. Use mode param with value 'a' to append. The code below opens an existing file and adds data from the DataFrame to the specified sheet.


# Append DataFrame to existing excel file
with pd.ExcelWriter('Courses.xlsx',mode='a') as writer:  
    df.to_excel(writer, sheet_name='Technologies')

4. Save Selected Columns

use param columns to save selected columns from DataFrame to excel file. The below example only saves columns Fee, Duration to excel file.


# Save Selected Columns to Excel File
df.to_excel('Courses.xlsx', columns = ['Fee','Duration'])

Use header param with a list of values if you wanted to write with different column names.

5. Skip Index

To skip Index from writing use index=False param. By default, it is set to True meaning write numerical Index to excel sheet.


# Skip Index
df.to_excel('Courses.xlsx', index = False)

Conclusion

In this article, you have learned how to write pandas DataFrame to excel file by using to_excel(). Also explored how to write to specific sheets, multiple sheets, and append to existing excel file.

Related Articles

  • pandas ExcelWriter Usage with Examples
  • pandas write CSV file
  • pandas read Excel
  • Pandas ExcelWriter Explained with Examples
  • Pandas Read Multiple CSV Files into DataFrame
  • How to Read Excel Multiple Sheets in Pandas
  • Pretty Print Pandas DataFrame or Series?
  • Pandas Handle Missing Data in Dataframe
  • How to read CSV without headers in pandas

References

  • https://stackoverflow.com/questions/38074678/append-existing-excel-sheet-with-new-dataframe-using-python-pandas/38075046#38075046

Excel files can be a great way of saving your tabular data particularly when you want to display it (and even perform some formatting to it) in a nice GUI like Microsoft Excel. In this tutorial, we’ll look at how to save a pandas dataframe to an excel .xlsx file.

Note: The terms “excel file” and “excel workbook” are used interchangeably in this tutorial.

The to_excel() function

The pandas DataFrame to_excel() function is used to save a pandas dataframe to an excel file. It’s like the to_csv() function but instead of a CSV, it writes the dataframe to a .xlsx file. The following is its syntax:

df.to_excel("pathfile_name.xlsx")

Here, df is a pandas dataframe and is written to the excel file file_name.xlsx present at the location path. By default, the dataframe is written to Sheet1 but you can also give custom sheet names. You can also write to multiple sheets in the same excel workbook as well (See the examples below).

Note that once the excel workbook is saved, you cannot write further data without rewriting the whole workbook.

Examples

First, we’ll create a sample dataframe that we’ll be using throughout this tutorial.

import pandas as pd

data = {
    'Name': ['Microsoft Corporation', 'Google, LLC', 'Tesla, Inc.',
             'Apple Inc.', 'Netflix, Inc.'],
    'Symbol': ['MSFT', 'GOOG', 'TSLA', 'AAPL', 'NFLX'],
    'Shares': [100, 50, 150, 200, 80]
}

# create dataframe from data
df = pd.DataFrame(data)
# display the dataframe
df

pandas dataframe containing information on a stock portfolio

Now, let’s look at examples of some of the different use-cases where the to_excel() function might be useful.

1. Save dataframe to an excel file with default parameters

df.to_excel("portfolio.xlsx")

If you just pass the file name to the to_excel() function and use the default values for all the other parameters, the resulting Excel file gets saved in your current working directory with the given file name. Here’s a snapshot of the file when opened in Excel.

Snapshot of the excel file saved

You can see that by default, the dataframe is saved to the sheet Sheet1. Also, note that the index of the dataframe is saved as a separate column. Pass index=False if you don’t want the index as a separate column in the excel file.

# to not include index as a column
df.to_excel("portfolio.xlsx", index=False)

Here’s how the saved excel file looks now.

Snapshot of the saved pandas dataframe without index in excel.

2. Save dataframe to an excel file with custom sheet name

You can specify the name of the worksheet using the sheet_name parameter.

# with custom sheet name
df.to_excel("portfolio.xlsx", sheet_name="stocks")

Snapshot of the saved dataframe with custom worksheet name

You can see in the above snapshot that the resulting excel file has stocks as its sheet name.

3. Save to multiple sheets in the same workbook

You can also save dataframes to multiple worksheets within the same workbook using the to_excel() function. For this, you need to specify an ExcelWriter object which is a pandas object used to write to excel files. See the example below:

# write to multiple sheets
df2 = df.copy()
with pd.ExcelWriter("portfolio.xlsx") as writer:
    df.to_excel(writer, sheet_name="stocks1")
    df2.to_excel(writer, sheet_name="stocks2")

Here’s how the saved excel file looks.

Snapshot of excel file with dataframes saved to two worksheets

In the above example, an ExcelWriter object is used to write the dataframes df and df2 to the worksheets stocks1 and stocks2 respectively.

Note that creating an ExcelWriter object with a file name that already exists will result in the contents of the existing file being erased.

For more on the pandas dataframe to_excel() function, refer to its official documentation.

You might also be interested in –

  • Write a Pandas DataFrame to a JSON File
  • Copy Pandas DataFrame to the Clipboard
  • Save Pandas DataFrame to a CSV file

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

    View all posts

Понравилась статья? Поделить с друзьями:
  • Python pandas save to excel
  • Python pandas reading excel
  • Python pandas read from excel
  • Python pandas read excel sheet
  • Python excel utf 8