0
0
Data Analysis Pythondata~5 mins

Reading Excel files (read_excel) in Data Analysis Python

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 the data.
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
Data Analysis Python
pandas.read_excel(io, sheet_name=0, header=0, usecols=None)

io is the file path or Excel file object.

sheet_name chooses which sheet to read; default is the first sheet.

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

This code first creates a small Excel file with sample data, then reads it back into Python and prints it.

Data Analysis Python
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

Make sure you have the openpyxl package installed to read .xlsx files.

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

Summary

read_excel loads Excel data into a DataFrame for easy analysis.

You can select sheets and columns to read only the data you need.

This is useful to work with Excel data directly in Python without manual copying.