Pandas read_excel(): Read Excel Files in Python
The Pandas read_excel() function imports data from an Excel workbook into a DataFrame. It is useful when working with spreadsheets containing sales records, student marks, customer details, financial reports, survey responses or other tabular information.
This tutorial explains how to read Excel files in Pandas, choose a worksheet, import particular columns or rows, work with headers, handle missing values, set data types and troubleshoot common errors. Examples use the modern .xlsx format and can be adapted for your own files in Jupyter Notebook or Python scripts.
If you are new to the library, begin with the Pandas DataFrame tutorial. You may also want to learn how to read CSV files, select rows and columns, and handle missing values.
- What does read_excel() do?
- Install the required Excel engine
- Read an Excel file
- Read a particular worksheet
- Read all worksheets
- Handle headers and column names
- Read selected columns and rows
- Set data types and parse dates
- Handle missing values
- Inspect and clean imported data
- Useful read_excel() parameters
- Common errors and solutions
- Practice exercises with solutions
- Frequently asked questions
1. What does Pandas read_excel() do?
pd.read_excel() reads a worksheet from an Excel workbook and normally returns a Pandas DataFrame. The DataFrame makes it possible to filter records, calculate summaries, find missing values, group data and prepare reports.
Import Pandas using its standard alias:
import pandas as pd
The basic pattern is:
df = pd.read_excel("sales.xlsx")
Here, sales.xlsx is the workbook file and df stores the imported data. By default, Pandas reads the first worksheet and treats the first row as column headings.
2. Install the required Excel engine
For modern Excel files with the .xlsx extension, Pandas commonly uses the openpyxl package. Install it in the same Python environment that runs your notebook or script:
pip install pandas openpyxl
In a Jupyter Notebook, you can run this as a notebook command:
%pip install pandas openpyxl
.xls files may require a different compatible engine.3. Read an Excel file into a DataFrame
Suppose your workbook is named sales.xlsx and contains the following worksheet:
| OrderID | Product | Region | Units | Revenue |
|---|---|---|---|---|
| 1001 | Laptop | North | 2 | 1700 |
| 1002 | Mouse | South | 5 | 125 |
| 1003 | Monitor | West | 1 | 300 |
Read it with the following code:
import pandas as pd
df = pd.read_excel("sales.xlsx")
print(df)
The returned DataFrame contains the worksheet rows and columns. The file must exist at the specified location and must be accessible to the Python process.
4. Read a particular worksheet by name
An Excel workbook can contain multiple worksheets, such as Sales, Customers and Expenses. Use sheet_name to choose the worksheet you need.
df = pd.read_excel("business.xlsx", sheet_name="Sales")
Worksheet names must match the workbook, including spaces. For example:
df = pd.read_excel("business.xlsx", sheet_name="Quarter 1 Sales")
You can also select a worksheet by its zero-based position. Position 0 is the first sheet, 1 is the second, and so on:
df = pd.read_excel("business.xlsx", sheet_name=1)
Use the sheet name when possible because worksheet positions can change when someone reorders the workbook.
5. Read all worksheets from an Excel workbook
Set sheet_name=None to load every worksheet. Pandas returns a dictionary in which each key is a worksheet name and each value is a DataFrame.
sheets = pd.read_excel("business.xlsx", sheet_name=None)
print(sheets.keys())
sales_df = sheets["Sales"]
Loop through the dictionary to inspect each worksheet:
for sheet_name, data in sheets.items():
print(sheet_name)
print(data.head())
Loading all sheets may use substantial memory if the workbook is large. If you need only one sheet, specify its name instead.
6. Handle headers and column names
Read a sheet that has no header row
When the first row contains data rather than headings, use header=None. Pandas will assign numeric column labels such as 0, 1 and 2.
df = pd.read_excel("raw_data.xlsx", header=None)
Provide your own column names
Use names with header=None when you want to supply labels for a file without headings:
df = pd.read_excel(
"raw_data.xlsx",
header=None,
names=["Name", "Department", "Salary"]
)
Skip title rows above the table
Some workbooks contain a report title or notes before the actual table. Use skiprows to skip those rows:
df = pd.read_excel("report.xlsx", skiprows=2)
Adjust the number based on the workbook layout. Inspect the result after import to confirm that the correct row became the header.
7. Read selected columns and rows
Select columns using usecols
Use usecols to import only the columns needed for your analysis. This can make the code clearer and reduce memory use.
df = pd.read_excel(
"sales.xlsx",
usecols=["OrderID", "Product", "Revenue"]
)
You can also use Excel column letters when appropriate:
df = pd.read_excel("sales.xlsx", usecols="A,C,E")
Skip rows and limit the number of rows
Use skiprows to ignore rows and nrows to read a limited number of data rows:
df = pd.read_excel("sales.xlsx", skiprows=2, nrows=100)
Remember that skiprows changes which row Pandas sees as the header unless you explicitly configure header or names.
8. Set data types and work with dates
Excel stores values in cells, but you may need to ensure that identifiers, quantities, dates or monetary values have the expected representation in Pandas.
Set column data types
df = pd.read_excel(
"customers.xlsx",
dtype={"CustomerID": "string", "Postcode": "string"}
)
String types can be useful for identifiers that contain leading zeros. Do not convert a column to a numeric type if its values are labels rather than quantities.
Convert a date column
After import, convert a date column explicitly when necessary:
df["OrderDate"] = pd.to_datetime(df["OrderDate"], errors="coerce")
With errors="coerce", values that cannot be interpreted as dates become NaT. Check those values before deciding how to handle them.
9. Handle missing values
Blank spreadsheet cells are commonly imported as missing values. You can inspect how many missing entries each column contains:
df = pd.read_excel("sales.xlsx")
print(df.isna().sum())
To remove rows that have missing values in a particular column:
df = df.dropna(subset=["Revenue"])
To fill missing values in a numeric column with its median:
df["Revenue"] = df["Revenue"].fillna(df["Revenue"].median())
10. Inspect and clean imported data
After reading an Excel workbook, inspect the DataFrame before performing analysis:
print(df.head())
print(df.shape)
print(df.columns)
print(df.dtypes)
print(df.info())
These checks help you confirm the number of rows and columns, the column labels, data types and missing-value patterns. If column names contain extra spaces, clean them carefully:
df.columns = df.columns.astype(str).str.strip()
Once the data is ready, you can filter rows, sort values, use groupby() for summaries, or count categories with value_counts().
11. Useful read_excel() parameters
| Parameter | Purpose | Example |
|---|---|---|
sheet_name | Select worksheet by name or position; use None for all sheets. | sheet_name="Sales" |
header | Choose the row used for column labels; use None if there is no header. | header=None |
names | Supply column labels, commonly with header=None. | names=["A", "B"] |
usecols | Import only selected columns. | usecols="A,C" |
skiprows | Skip rows at the start or specified row positions. | skiprows=2 |
nrows | Limit how many rows are read. | nrows=50 |
dtype | Request data types for selected columns. | dtype={"ID": "string"} |
na_values | Specify additional values that should be treated as missing. | na_values=["N/A", "-"] |
engine | Choose a compatible workbook-reading engine when needed. | engine="openpyxl" |
12. Common errors and solutions
FileNotFoundError
Cause: Python cannot find the workbook at the supplied path.
Solution: Check the spelling, file extension and working directory. Use an absolute path if necessary.
from pathlib import Path
print(Path("sales.xlsx").resolve())
print(Path("sales.xlsx").exists())
ImportError or missing optional dependency
Cause: The required Excel engine is not installed in the active Python environment.
Solution: For typical .xlsx files, install openpyxl using %pip install openpyxl in Jupyter or pip install openpyxl in a terminal.
Worksheet not found
Cause: The value passed to sheet_name does not match a worksheet name.
Solution: Check the spelling and spaces in Excel. You can list worksheet names with:
xls = pd.ExcelFile("business.xlsx")
print(xls.sheet_names)
Unexpected headers or columns
Cause: The workbook contains title rows, merged cells or a header row different from the default.
Solution: Inspect the sheet in Excel, then adjust header, skiprows or names. Validate the imported column names before continuing.
Numbers or dates imported in an unexpected format
Cause: Cells may contain mixed formats, text, formulas or locale-specific values.
Solution: Inspect the actual values and data types, then use explicit conversions such as pd.to_numeric() or pd.to_datetime() where appropriate.
13. Practice exercises with solutions
Assume a workbook named students.xlsx has a worksheet named Results with columns Student, Class and Marks.
Exercise 1: Read the Results worksheet
Write code to import the Results worksheet.
Show solution
df = pd.read_excel("students.xlsx", sheet_name="Results")
Exercise 2: Import only Student and Marks
Read only the two specified columns.
Show solution
df = pd.read_excel(
"students.xlsx",
sheet_name="Results",
usecols=["Student", "Marks"]
)
Exercise 3: Read the workbook without using its first row as headers
Show solution
df = pd.read_excel("students.xlsx", sheet_name="Results", header=None)
Exercise 4: Check for missing values
Show solution
print(df.isna().sum())
Exercise 5: Read the first 20 data rows
Show solution
df = pd.read_excel("students.xlsx", sheet_name="Results", nrows=20)
14. Frequently asked questions
How do I read an Excel file in Pandas?
Import Pandas and call pd.read_excel("file.xlsx"). Ensure that the file path is correct and that a compatible Excel engine is installed.
How do I read a specific sheet in Pandas?
Use the sheet_name argument, for example pd.read_excel("file.xlsx", sheet_name="Sales").
How can I read every sheet in an Excel workbook?
Pass sheet_name=None. Pandas returns a dictionary of DataFrames keyed by worksheet name.
Do I need openpyxl to read Excel files?
For many modern .xlsx workbooks, Pandas uses openpyxl. Other Excel formats may require a different engine, so install the package compatible with your file type and environment.
What is the difference between read_csv() and read_excel()?
read_csv() reads delimited text files, while read_excel() reads workbook sheets. Choose the function based on the format of your source file.
Can Pandas write data back to Excel?
Yes. Use DataFrame.to_excel() to export a DataFrame. Writing a workbook has different options and engine requirements from reading one.
How do I read only some Excel columns?
Use usecols, for example pd.read_excel("file.xlsx", usecols=["Name", "Score"]).
Conclusion
Pandas read_excel() is a convenient way to import spreadsheet data for Python analysis. Start with the correct file path and worksheet, then check headers, column types and missing values before drawing conclusions from the data.
Continue learning with Pandas read_csv(), filtering a DataFrame, groupby() and the complete Pandas tutorial.
