pandas
iPython
xlsx
data-analysis
python-libraries

How to read a .xlsx file using the pandas Library in iPython?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To read an Excel .xlsx file in IPython or Jupyter, the standard tool is pandas.read_excel. In most modern environments, pandas uses openpyxl as the engine for .xlsx files, so the job is usually just installing the dependency and pointing read_excel at the file.

Read a Basic .xlsx File

The simplest case looks like this:

python
1import pandas as pd
2
3df = pd.read_excel("sales.xlsx")
4print(df.head())

If the file is in the current notebook directory, that is enough. In IPython, you can confirm your current working directory with:

python
import os
print(os.getcwd())

That helps when the file exists but pandas cannot find it because the notebook kernel started in a different directory than you expected.

Install the Excel Engine If Needed

If read_excel raises an import error for openpyxl, install it into the same environment that runs IPython:

python
%pip install openpyxl

Then retry:

python
import pandas as pd

df = pd.read_excel("sales.xlsx", engine="openpyxl")

Explicitly setting engine="openpyxl" is not always required, but it can make the code more obvious and reduce confusion when multiple Excel engines are installed.

Read Specific Sheets or Columns

Excel files often contain multiple sheets. You can select one by name:

python
df = pd.read_excel("sales.xlsx", sheet_name="Q1", engine="openpyxl")
print(df.head())

Or read all sheets at once:

python
all_sheets = pd.read_excel("sales.xlsx", sheet_name=None, engine="openpyxl")
print(all_sheets.keys())

sheet_name=None returns a dictionary mapping sheet names to data frames.

You can also limit which columns to load:

python
1df = pd.read_excel(
2    "sales.xlsx",
3    sheet_name="Q1",
4    usecols=["date", "revenue", "region"],
5    engine="openpyxl"
6)

That is helpful when the workbook is large and you only need a few fields.

Clean Data as You Load It

Excel data often contains date columns, custom headers, or skipped rows. read_excel can handle many of those issues directly:

python
1df = pd.read_excel(
2    "sales.xlsx",
3    sheet_name="Q1",
4    parse_dates=["date"],
5    skiprows=1,
6    engine="openpyxl"
7)

Doing basic cleanup at load time usually makes the notebook much easier to work with afterward.

Use IPython for Fast Inspection

Once the file is loaded, IPython makes quick inspection easy:

python
df.info()
df.describe(include="all")
df.sample(5)

That lets you catch missing values, surprising column types, or header problems early instead of discovering them halfway through the analysis.

Read from an Absolute Path When Notebooks Move Around

Notebook kernels are often started from different folders depending on how Jupyter was launched. If relative paths become unreliable, pass an absolute path instead:

python
df = pd.read_excel("/Users/you/data/sales.xlsx", engine="openpyxl")

That removes ambiguity and is especially useful when you share notebooks across machines or run them from scheduled jobs later.

Common Pitfalls

  • Installing openpyxl into a different Python environment than the one running IPython. Use %pip inside the notebook when in doubt.
  • Assuming the notebook directory matches the file location. Check os.getcwd() or pass an absolute path.
  • Reading the wrong sheet because the workbook has multiple tabs and the default first sheet is not the one you intended.
  • Letting Excel header rows or skipped lines become part of the data accidentally. Use header, skiprows, and usecols deliberately.
  • Expecting Excel cell formatting to survive as analysis-ready types automatically. Always inspect the resulting data frame after loading.

Summary

  • Use pandas.read_excel to load .xlsx files in IPython or Jupyter.
  • Install openpyxl if the environment does not already have an Excel engine.
  • Pick the right sheet, columns, and parsing options when the workbook is more complex than a simple table.
  • Confirm the notebook working directory if the file cannot be found.
  • Inspect the loaded data frame immediately so type and header issues do not surprise you later.

Course illustration
Course illustration

All Rights Reserved.