How to read specific sheet content when there are multiple sheets in an excel file?

Given a Pandas DataFrame, we have to read specific sheet content when there are multiple sheets in an excel file. By Pranit Sharma Last updated : September 21, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and the data.
Pandas DataFrames can be created with the help of dictionaries or arrays but in real-world analysis, first, a csv file or an xlsx file is imported and then the content of csv or excel file is converted into a DataFrame.

Problem statement

An excel file may contain multiple sheets which may contain important data to analyze. Here, we are going to learn how to read a specific sheet from an excel file?

Import an Excel file

An xlsx file is a spreadsheet file that can be created with certain software, especially with the popularly known MS Excel. To import an Excel file, we use the following piece of code:

pd.ExcelFile('Path_of_the_file')

Read specific sheet content

To read specific sheet content, we will use the file.parse() method by passing the sheet name whose content we have to read. Where, file an object that contains the data of the excel fil.

Let us understand with the help of an example,

Python program to read specific sheet content

# Importing pandas package
import pandas as pd

# Importing Excel file
file = pd.ExcelFile('D:/Book1.xlsx')

# Display the content of a particular sheet
print(file.parse('A'))

Output

Example 1: Read specific sheet content

We can also read other sheets; suppose we have sheet file named 'B'. The reading of file 'B' is explained with the following piece of code:

# Display the content of another sheet named "B"
print(file.parse('B'))

Output

Example 2: Read specific sheet content

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.