Reading Excel Files with Pandas | Generated by AI

Home 2025.10

Prerequisites

To read Excel files in Python, you need the pandas library and an Excel engine. Install pandas if not already installed:

pip install pandas

For modern Excel formats (.xlsx), install openpyxl:

pip install openpyxl

For .xls files, use xlrd:

pip install xlrd

Basic Usage

Pandas provides the pd.read_excel() function to load Excel data into a DataFrame.

  1. Import pandas:
    import pandas as pd
    
  2. Read the Excel file (specify the file path):
    df = pd.read_excel('path/to/your/file.xlsx')
    
    • This loads the first sheet by default.
    • The result is a DataFrame containing the Excel data.
  3. Inspect the data:
    print(df.head())  # View first 5 rows
    print(df.info())  # Summary of columns and data types
    

Advanced Options

Example

Suppose you have an Excel file sales.xlsx with a sheet named “Data” containing sales data.

import pandas as pd

# Read the specific sheet
df = pd.read_excel('sales.xlsx', sheet_name='Data')

# Display the DataFrame
print(df)

# Save to CSV if needed
df.to_csv('sales.csv', index=False)

For more details, refer to the pandas documentation on read_excel. If you encounter errors, check the Excel file format and ensure the required packages are installed.


Back

x-ai/grok-code-fast-1

Donate