Q&A library Python

How do I read an Excel file with pandas?

Python Intermediate ioread-excelsheets

Short answer

Use pd.read_excel, choosing the sheet with sheet_name, and pass sheet_name=None to load every sheet as a dictionary of DataFrames. You need an engine like openpyxl installed for xlsx files.

The short version

Use pd.read_excel("file.xlsx") to read a spreadsheet. By default it reads the first sheet; pass sheet_name to pick another, or sheet_name=None to load every sheet into a dictionary of DataFrames. You need a reader engine installed, usually openpyxl for modern .xlsx files.

Reading a single sheet

import pandas as pd

df = pd.read_excel("orders.xlsx")                    # first sheet
df = pd.read_excel("orders.xlsx", sheet_name="Q1")   # a named sheet
df = pd.read_excel("orders.xlsx", sheet_name=1)      # the second sheet, by position

sheet_name accepts a name or a zero-based position, so you can select the sheet however is convenient.

Reading every sheet at once

Passing sheet_name=None returns a dictionary mapping each sheet name to its DataFrame, which is the clean way to handle a multi-sheet workbook.

sheets = pd.read_excel("book.xlsx", sheet_name=None)
for name, frame in sheets.items():
    print(name, frame.shape)

You can then work with sheets["Q1"] and so on, or concatenate them if they share a layout.

The engine requirement

Reading .xlsx needs the openpyxl package installed, and older .xls files need xlrd. If you get an ImportError about a missing engine, install the right one.

pip install openpyxl

Pandas picks the engine automatically once it is installed, so you rarely need to specify it.

Common options

The same reading gotchas as CSV apply, plus a few Excel-specific ones.

pd.read_excel(
    "report.xlsx",
    sheet_name="data",
    skiprows=2,              # skip title rows above the header
    usecols="B:D",          # read only columns B to D
    dtype={"code": "string"},  # keep codes as text, not numbers
)

usecols accepts Excel-style column letters, which is handy when a sheet has extra columns you do not want. As with CSV, read identifier columns as text with dtype so leading zeros survive.

Checking after reading

Excel data is often messy, with merged cells, stray title rows and mixed types. After reading, run df.head() and df.dtypes to confirm the header landed on the right row and that numbers, dates and codes have sensible types. Catching a layout problem here is far cheaper than discovering it several steps later.

Writing back

To write a DataFrame to Excel, use df.to_excel("out.xlsx", index=False). For several sheets in one file, use pd.ExcelWriter as a context manager and call to_excel once per sheet, which keeps them all in a single workbook.

Need this solved on your own data?

The library is free and always will be. When the problem is bigger than an answer, we take on the work.

Start a project