Reading multiple Excel sheets in Pandas
Pandas is a well-known Python library for data science and machine learning. This library provides many functions for data analysis, prediction, and manipulation.
There are many operations we can perform on the datasets provided. Most of the time, we perform analysis on Excel files (also known as xls files) or CSV files (single spreadsheets) that contain the datasets.
We can load a single worksheet or multiple Excel worksheets from this file.
Reading Multiple Excel Worksheets from a Workbook Using Pandas in Python
To use Pandas, we should first install it using the following command.
#Python 3.x
pip install pandas
Also, we will read an Excel file here (with the extension xls). For this, we also have to install xlrd
the module using the following command.
#Python 3.x
pip install xlrd
Sometimes we have a large dataset consisting of multiple spreadsheets in the same workbook. But we are only interested in some specific spreadsheets of that file.
To do this, we have to open the specific spreadsheet from the workbook. We can do this task easily in Python using Pandas.
read_Excel()
Open the spreadsheet from the Pandas workbook using
First, we will pd.ExcelFile('path_to_file.xls')
read the entire Excel file using . Here, pd
referring to Pandas, we will pass the path of the Excel file as a parameter to ExcelFile()
the method call.
In the following code, we import the Pandas package. Then we read an Excel file dataset.xls
containing two spreadsheets iris
and customer_churn
.
We are working with Jupyter
a notebook, so we have uploaded this dataset to our home directory. xls
The object spreadsheet is now accessible.
If we only want to read iris
the spreadsheet, we will call read_excel()
the method again. In the first parameter, we will pass xls
and in the second parameter, we will write the name of the specific spreadsheet that we want to read from that xls file.
We then df.head()
displayed the first five rows of the spreadsheet using . We can read_excel()
load multiple spreadsheets from a workbook in the same way by specifying the name of the spreadsheet in .
# Python 3.x
import pandas as pd
xls = pd.ExcelFile("dataset.xls")
df = pd.read_excel(xls, "iris")
df.head()
print(df)
Output:
parse()
Open a spreadsheet from a workbook using the
Another way to read a single spreadsheet from a workbook is to load the xls file and then xls
call parse()
the method with the object. As parse()
a parameter of the method, we will specify the index number of the spreadsheet.
The integers refer to the spreadsheets in that xls file. 0
represents the first spreadsheet, 1
represents the second, and so on, just like an array index.
Here, we load iris
the spreadsheet, which is the second spreadsheet of our workbook, so we pass 1
as the index. The output of this code is the same as above.
# Python 3.x
import pandas as pd
xls = pd.ExcelFile("dataset.xls")
df = xls.parse(1)
df.head()
print(df)
Output:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding the installed version of Pandas
Publish Date:2025/04/12 Views:190 Category:Python
-
Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc
KeyError in Pandas
Publish Date:2025/04/12 Views:81 Category:Python
-
This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest
Grouping and Sorting in Pandas
Publish Date:2025/04/12 Views:90 Category:Python
-
This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies
Plotting Line Graph with Data Points in Pandas
Publish Date:2025/04/12 Views:65 Category:Python
-
Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize
Converting Timedelta to Int in Pandas
Publish Date:2025/04/12 Views:123 Category:Python
-
This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .
Pandas fill NaN values
Publish Date:2025/04/12 Views:93 Category:Python
-
This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50
Pandas Convert String to Number
Publish Date:2025/04/12 Views:147 Category:Python
-
This tutorial explains how to pandas.to_numeric() convert string values of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :
How to Change the Data Type of a Column in Pandas
Publish Date:2025/04/12 Views:139 Category:Python
-
We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method
Get the first row of Dataframe Pandas
Publish Date:2025/04/12 Views:78 Category:Python
-
This tutorial explains how to use the get_first_row pandas.DataFrame.iloc attribute and pandas.DataFrame.head() get_first_row method from a Pandas DataFrame. We will use the following DataFrame in the following example to explain how to get