How to Filter rows using Pandas Chaining?
In this article, we will learn how to filter rows using Pandas chaining. For this first we have to look into some previous terms which are given below :
- Pandas DataFrame: It is a two-dimensional data structure, i.e. the data is tabularly aligned in rows and columns. The Pandas DataFrame has three main components i.e. data, rows, and columns.
- Pandas Chaining: Method chaining, in which methods are called on an object sequentially, one after the another. It has always been a programming style that’s been possible with pandas, and over the past few releases, many methods have been introduced that allow even more chaining.
Creating Dataframe to Filter rows using Pandas Chaining
Python
import pandas as pd
data = pd.DataFrame(
{ 'ID' : { 0 : 105 , 1 : 102 , 2 : 101 , 3 : 106 , 4 : 103 , 5 : 104 , 6 : 107 },
'Name' : { 0 : 'Ram Kumar' , 1 : 'Jack Wills' , 2 : 'Deepanshu Rustagi' ,
3 : 'Thomas James' , 4 : 'Jenny Advekar' , 5 : 'Yash Raj' ,
6 : 'Raman Dutt Mishra' },
'Age' : { 0 : 40 , 1 : 23 , 2 : 20 , 3 : 34 , 4 : 18 , 5 : 56 , 6 : 35 },
'Country' : { 0 : 'India' , 1 : 'Uk' , 2 : 'India' , 3 : 'Australia' ,
4 : 'Uk' , 5 : 'India' , 6 : 'India' }
})
data
|
Output:
Filter by specific value
Method 1: Filter rows using eq
Here, we select the rows with a specific value in a particular column. The Country column in Dataframe is selected with the value ‘India’ to filter rows.
Python3
print (data[data.Country.eq( 'India' )])
|
Output:
Method 2: Filter rows using pipe
Here, we select the rows with a specific value in a particular column. The Country column in Dataframe is selected with the value ‘India’ to filter rows using a pipe.
Python3
df2 = data.pipe( lambda x: x[ 'Country' ] = = "India" )
print (df2)
|
Output:
0 True
1 False
2 True
3 False
4 False
5 True
6 True
Name: Country, dtype: bool
Filter by specific grouped values
Method 1: Filter rows using manually giving index value
Here, we select the rows with specific grouped values in a particular column. The Age column in Dataframe is selected with a value less than 30 to filter rows.
Output:
Method 2: Filter rows using loc
Here, we select the rows with specific grouped values in a particular column. The ID and Age column in Dataframe is selected with a value less than equal to 103 and Age equal to 23 to filter rows.
Python3
df2 = data.loc[ lambda x: x[ 'ID' ] < =
103 ].loc[ lambda x: x[ 'Age' ] = = 23 ]
print (df2)
|
Output:
ID Name Age Country
1 102 Jack Wills 23 Uk
Method 3: Filter rows using a mask
Here, we select the rows with specific grouped values in a particular column. The Age column in Dataframe is selected with a value greater than equal to 39 to filter rows.
Python3
df2 = data.mask( lambda x: x[ 'Age' ] < = 39 )
df2 = df2.dropna()
print (df2)
|
Output:
ID Name Age Country
0 105.0 Ram Kumar 40.0 India
5 104.0 Yash Raj 56.0 India
Filter by specific character or a specific set of values
Method 1: Filter rows using contains
Here, we select the rows with specific characters or string values in a particular column. The Name column in Dataframe is selected with a value containing ‘am’ to filter rows.
Python3
print (data[data.Name. str .contains( 'am' )])
|
Output:
Method 2: Filter rows using isin
Here, we select the rows from a specific set of values in a particular column. The Country column in Dataframe is selected and matched with the given set of values to filter rows.
Python3
lst = [ 'Uk' , 'Australia' ]
print (data[data.Country.isin(lst)])
|
Output:
Last Updated :
16 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...