Get Started

Listing sheets in Excel files

Whether you like it or not, any working data scientist will need to deal with Excel spreadsheets at some point in time. You won't always want to do so in Excel, however!

Here, you'll learn how to use pandas to import Excel spreadsheets and how to list the names of the sheets in any loaded .xlsx file.

Recall from the video that, given an Excel file imported into a variable spreadsheet, you can retrieve a list of the sheet names using the attribute spreadsheet.sheet_names.

Specifically, you'll be loading and checking out the spreadsheet 'battledeath.xlsx', modified from the Peace Research Institute Oslo's (PRIO) dataset. This data contains age-adjusted mortality rates due to war in various countries over several years.

This is a part of the course

“Introduction to Importing Data in Python”

View Course

Exercise instructions

  • Assign the spreadsheet filename (provided above) to the variable file.
  • Pass the correct argument to pd.ExcelFile() to load the file using pandas, assigning the result to the variable xls.
  • Print the sheetnames of the Excel spreadsheet by passing the necessary argument to the print() function.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import pandas
import pandas as pd

# Assign spreadsheet filename: file
file = ____

# Load spreadsheet: xls
xls = pd.ExcelFile(____)

# Print sheet names
print(____)

This exercise is part of the course

Introduction to Importing Data in Python

BeginnerSkill Level
4.7+
88 reviews

Learn to import data into Python from various sources, such as Excel, SQL, SAS and right from the web.

You've learned how to import flat files, but there are many other file types you will potentially have to work with as a data scientist. In this chapter, you'll learn how to import data into Python from a wide array of important file types. These include pickled files, Excel spreadsheets, SAS and Stata files, HDF5 files, a file type for storing large quantities of numerical data, and MATLAB files.

Exercise 1: Introduction to other file typesExercise 2: Not so flat any moreExercise 3: Loading a pickled fileExercise 4: Listing sheets in Excel files
Exercise 5: Importing sheets from Excel filesExercise 6: Customizing your spreadsheet importExercise 7: Importing SAS/Stata files using pandasExercise 8: How to import SAS7BDATExercise 9: Importing SAS filesExercise 10: Using read_stata to import Stata filesExercise 11: Importing Stata filesExercise 12: Importing HDF5 filesExercise 13: Using File to import HDF5 filesExercise 14: Using h5py to import HDF5 filesExercise 15: Extracting data from your HDF5 fileExercise 16: Importing MATLAB filesExercise 17: Loading .mat filesExercise 18: The structure of .mat in Python

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free