ZetCode

Pandas Reading and Writing Excel

last modified November 17, 2026 Python 3.15

Pandas can exchange data with Excel workbooks through the to_excel and read_excel functions. An Excel workbook is a file that can hold several sheets, each one a table of rows and columns, so it maps naturally onto Pandas DataFrames.

Pandas does not write Excel files by itself; it uses a separate library called an engine. We use openpyxl throughout this article, which must be installed first, for example with pip install openpyxl. For working with Excel files directly in Python, see the Python openpyxl article. The examples were verified with Pandas 3.1.1 and openpyxl 3.1.5.

Writing and reading a workbook

Calling to_excel on a DataFrame writes it to a new .xlsx file; read_excel reads it back into a DataFrame. The index=False argument prevents the row index from being written as an extra column, which is usually what we want for a clean table.

excel_basic.py
import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [30, 25]})
df.to_excel('people.xlsx', index=False)

back = pd.read_excel('people.xlsx')
print(back)

The file people.xlsx is created in the current directory. The default sheet is named Sheet1. Reading the file back gives a DataFrame with the same two columns and values, which shows that the round trip preserved the data.

$ python excel_basic.py
    Name  Age
0  Alice   30
1    Bob   25

Choosing a sheet with sheet_name

The sheet_name parameter of read_excel selects which sheet to load. It accepts a sheet name as a string or a zero-based position as an integer. Without it, Pandas reads the first sheet.

excel_sheet.py
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2]})
df2 = pd.DataFrame({'B': [3, 4]})

with pd.ExcelWriter('sheets2.xlsx', engine='openpyxl') as writer:
    df1.to_excel(writer, sheet_name='first', index=False)
    df2.to_excel(writer, sheet_name='second', index=False)

print('by name:')
print(pd.read_excel('sheets2.xlsx', sheet_name='second'))
print()

print('by position:')
print(pd.read_excel('sheets2.xlsx', sheet_name=0))

The first read uses the name 'second' and returns the B column; the second uses the position 0 and returns the A column from the first sheet. The short with block at the top creates a workbook with two named sheets; the next section explains it in detail.

$ python excel_sheet.py
by name:
   B
0  3
1  4

by position:
   A
0  1
1  2

Writing multiple sheets with ExcelWriter

A single call to to_excel writes one sheet. To put several DataFrames into one workbook, we create an ExcelWriter and pass it to each to_excel call with a different sheet_name. Using it in a with statement makes sure the file is saved and closed properly.

excel_writer.py
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2]})
df2 = pd.DataFrame({'B': [3, 4]})

with pd.ExcelWriter('sheets.xlsx', engine='openpyxl') as writer:
    df1.to_excel(writer, sheet_name='first', index=False)
    df2.to_excel(writer, sheet_name='second', index=False)

print('sheet names:', pd.ExcelFile('sheets.xlsx').sheet_names)

Both DataFrames are written into the same workbook, and reading its sheet_names property confirms that the two sheets, first and second, exist. This is a common way to package related tables into a single Excel file.

$ python excel_writer.py
sheet names: ['first', 'second']

The index argument

By default to_excel writes the row index as an unnamed first column. When the workbook is read back, that column appears with the name Unnamed: 0. Passing index=False omits it so the file contains only the real data columns.

excel_index.py
import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [30, 25]})

df.to_excel('with_index.xlsx')
df.to_excel('no_index.xlsx', index=False)

print('default (index written):')
print(pd.read_excel('with_index.xlsx'))
print()
print('index=False:')
print(pd.read_excel('no_index.xlsx'))

The first workbook has an extra Unnamed: 0 column holding the numbers 0 and 1. The second workbook has only Name and Age. If the index carries meaningful labels, keep it; otherwise index=False produces a cleaner file.

$ python excel_index.py
default (index written):
   Unnamed: 0   Name  Age
0           0  Alice   30
1           1    Bob   25

index=False:
    Name  Age
0  Alice   30
1    Bob   25

Selecting columns

The usecols parameter of read_excel limits the columns loaded from a sheet, just as it does for CSV files. It accepts column names or Excel-style letters such as 'A:C'.

excel_columns.py
import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [30, 25], 'City': ['Prague', 'Bratislava']})
df.to_excel('cols.xlsx', index=False)

print('all columns:')
print(pd.read_excel('cols.xlsx'))
print()
print('selected columns:')
print(pd.read_excel('cols.xlsx', usecols=['Name', 'City']))

The workbook stores all three columns, but the second read loads only Name and City. This is useful when a sheet is wide and we are interested in just a few of its fields. The same idea is covered for text files in the article on reading and writing CSV.

$ python excel_columns.py
all columns:
    Name  Age        City
0  Alice   30      Prague
1    Bob   25  Bratislava

selected columns:
    Name        City
0  Alice      Prague
1    Bob  Bratislava

The openpyxl engine

Pandas selects an Excel engine automatically when one is installed. We can also name it explicitly with the engine parameter on both writing and reading. openpyxl reads and writes the modern .xlsx format and must be installed separately; it is the engine used in all examples here.

excel_engine.py
import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [30, 25]})
df.to_excel('engine.xlsx', engine='openpyxl', index=False)

with pd.ExcelFile('engine.xlsx', engine='openpyxl') as xls:
    print('sheet names:', xls.sheet_names)
    print(pd.read_excel(xls))

The ExcelFile object opens the workbook once and lets us inspect it; here we print its sheet names and then pass the open object to read_excel. Reusing an ExcelFile avoids opening the same workbook repeatedly when reading several sheets. If openpyxl is missing, Pandas raises an ImportError telling us to install it.

$ python excel_engine.py
sheet names: ['Sheet1']
    Name  Age
0  Alice   30
1    Bob   25

Source

Pandas documentation - read_excel
Pandas documentation - to_excel
Pandas documentation - ExcelWriter
openpyxl documentation

In this article we have written Excel workbooks with to_excel, read them with read_excel, chosen individual sheets, combined several DataFrames with ExcelWriter, controlled the row index, selected columns, and worked explicitly with the openpyxl engine. See also the reading and writing CSV, Pandas DataFrame, and Python openpyxl articles.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pandas tutorials.