How to Read an Excel File in Python?

In this tutorial, I will explain how to read an Excel file in Python using the pandas library. As a programmer, we often come across reading files, and reading Excel files is necessary when dealing with Python files. I will explain reading an Excel file with examples and screenshots.

Read an Excel File in Python

Let us learn more about reading an Excel file in Python.

Read How to Import a Python File from the Same Directory?

Example:

Let’s start with a basic example. Suppose you have an Excel file named sales_data.xlsx with a sheet named Q1_Sales. Here’s how you can read this file:

import pandas as pd

# Load the Excel file
file_path = 'path/to/your/sales_data.xlsx'
sheet_name = 'Q1_Sales'

# Read the Excel file
df = pd.read_excel(file_path, sheet_name=sheet_name)

# Display the first few rows of the DataFrame
print(df.head())

Output:

         Date  Sales     Location
0  2024-01-01    500     New York
1  2024-01-02    750      Chicago
2  2024-01-03    600  Los Angeles

You can see the output in the screenshot below.

Read an Excel File in Python

In this example, pandas.read_excel() is used to read the Excel file into a DataFrame. The sheet_name parameter specifies which sheet to read.

Check out How to Get File Size in Python?

Read Multiple Sheets

If your Excel file contains multiple sheets and you need to read them all, you can do so using a dictionary of DataFrames:

# Read all sheets
dfs = pd.read_excel(file_path, sheet_name=None)

# Display the names of the sheets
print(dfs.keys())

# Access a specific sheet
print(dfs['Q1_Sales'].head())

Output:

dict_keys(['Q1_Sales'])
         Date  Sales     Location
0  2024-01-01    500     New York
1  2024-01-02    750      Chicago
2  2024-01-03    600  Los Angeles

You can see the output in the screenshot below.

How to Read an Excel File in Python

Here, setting sheet_name=None reads all sheets into a dictionary where the keys are the sheet names and the values are the DataFrames.

Read How to Overwrite a File in Python?

Handle Missing Data

Real-world data often contains missing values. You can handle these using pandas:

df = pd.read_excel(file_path, sheet_name=sheet_name)

# Fill missing values with a specific value
df_filled = df.fillna(0)

# Drop rows with missing values
df_dropped = df.dropna()

print(df_filled.head())
print(df_dropped.head())

Output:

dict_keys(['Q1_Sales'])
         Date  Sales     Location
0  2024-01-01    500     New York
1  2024-01-02    750      Chicago
2  2024-01-03    600  Los Angeles
         Date  Sales     Location
0  2024-01-01    500     New York
1  2024-01-02    750      Chicago
2  2024-01-03    600  Los Angeles
         Date  Sales     Location
0  2024-01-01    500     New York
1  2024-01-02    750      Chicago
2  2024-01-03    600  Los Angeles

You can see the output in the screenshot below.

Read an Excel File in Python Handle Missing Data

Check out How to Rename Files in Python?

Read Specific Columns

If you only need a subset of the columns, you can specify them using the usecols parameter:

columns_to_read = ['Date', 'Revenue']

df = pd.read_excel(file_path, sheet_name=sheet_name, usecols=columns_to_read)

print(df.head())

Parse Dates

When your data includes date columns, you can ensure they are parsed correctly:

df = pd.read_excel(file_path, sheet_name=sheet_name, parse_dates=['Date'])

print(df.head())
print(df.info())

Read How to Check if a File is Empty in Python?

Example: Analyze Sales Data

Let’s put it all together with a more comprehensive example. Suppose you want to analyze the sales data for the first quarter. Your Excel file contains columns for date, region, manager, product, units sold, and revenue.

# Load the Excel file
file_path = 'path/to/your/sales_data.xlsx'
sheet_name = 'Q1_Sales'

# Read the Excel file
df = pd.read_excel(file_path, sheet_name=sheet_name, parse_dates=['Date'])

# Fill missing values
df = df.fillna({'Units Sold': 0, 'Revenue': 0})

# Calculate total revenue per region
total_revenue_per_region = df.groupby('Region')['Revenue'].sum()

print(total_revenue_per_region)

In this example, we read the Excel file, parsed the date column, filled missing values, and calculated the total revenue per region.

Check out How to Get File Name Without Extension in Python?

Conclusion

In this tutorial, I explained how to read an Excel file in Python. I discussed an example of reading an Excel file, how to read multiple sheets, handling missing data, reading specific columns, parse dates, and example to analyze sales data.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.