Home »
Python »
Python Programs
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.
Submitted by Pranit Sharma, on June 01, 2022
Given a Pandas DataFrame, we have to read specific sheet content when there are multiple sheets in an excel file.
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.
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')
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?
To work with pandas, we need to import pandas package first, below is the syntax:
import pandas as pd
Let us understand with the help of an example,
# 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:
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:
Python Pandas Programs »