0
0
Pandasdata~5 mins

Reading Excel files with read_excel in Pandas

Choose your learning style9 modes available
Introduction

We use read_excel to open and read data stored in Excel files so we can analyze it in Python.

You have sales data saved in an Excel file and want to analyze monthly trends.
You received survey results in Excel and want to clean and summarize them.
You want to combine data from multiple Excel sheets into one table.
You need to quickly check the contents of an Excel file without opening Excel software.
Syntax
Pandas
pandas.read_excel(io, sheet_name=0, header=0, usecols=None, nrows=None)

io is the file path or Excel file object.

sheet_name lets you pick which sheet to read (default is the first sheet).

Examples
Reads the first sheet of data.xlsx into a DataFrame.
Pandas
import pandas as pd
df = pd.read_excel('data.xlsx')
Reads the sheet named 'Sales' from the Excel file.
Pandas
df = pd.read_excel('data.xlsx', sheet_name='Sales')
Reads only columns A to C from the first sheet.
Pandas
df = pd.read_excel('data.xlsx', usecols='A:C')
Reads only the first 10 rows from the first sheet.
Pandas
df = pd.read_excel('data.xlsx', nrows=10)
Sample Program

This code creates a small Excel file with names, ages, and cities, then reads it back into a DataFrame and prints it.

Pandas
import pandas as pd

# Create a sample Excel file
sample_data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['NY', 'LA', 'Chicago']}
df_sample = pd.DataFrame(sample_data)
df_sample.to_excel('sample.xlsx', index=False)

# Read the Excel file
read_df = pd.read_excel('sample.xlsx')
print(read_df)
OutputSuccess
Important Notes

If you get an error reading Excel files, make sure you have openpyxl installed for .xlsx files.

You can read multiple sheets by passing a list to sheet_name.

Summary

read_excel loads Excel data into pandas for easy analysis.

You can choose sheets, columns, and rows to read.

It helps work with Excel data without needing Excel software.