ADVERTISEMENT
ADVERTISEMENT

How to filter pandas DataFrame by operator chaining?

Given a DataFrame, we have to filter it by operator chaining.
Submitted by Pranit Sharma, on April 30, 2022

Pandas is a special tool which 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 structure in pandas. DataFrames consists of rows, columns and the data. Certain operation can be performed on DataFrames.

Chaining is a programming method in which we pass call methods sequentially one after another. Operator chaining refers to applying different operators like equal to (==), less than (<), greater than (>) etc. Here we are going to learn how to filter DataFrame by operator chaining. We are going to apply different operator checks on our Data and try to filter it.

To work with Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

Let us understand with the help of an example.

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    'Roll_no': [ 1,2,3,4,5],
    'Name': ['Abhishek', 'Babita','Chetan','Dheeraj','Ekta'],
    'Gender': ['Male','Female','Male','Male','Female'],
    'Marks': [50,66,76,45,80],
    'Standard': ['Fifth','Fourth','Third','Third','Third']
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df)

Output:

Original DataFrame:
    Roll_no      Name  Gender  Marks Standard
0        1  Abhishek    Male     50    Fifth
1        2    Babita  Female     66   Fourth
2        3    Chetan    Male     76    Third
3        4   Dheeraj    Male     45    Third
4        5      Ekta  Female     80    Third

Performing certain operations and filtering Data

# Performing filter operations
# Select the rows with specific value
print(df[df.Standard.eq('Third')])

Output:

   Roll_no     Name  Gender  Marks Standard
2        3   Chetan    Male     76    Third
3        4  Dheeraj    Male     45    Third
4        5     Ekta  Female     80    Third
# Performing filter operations
# Select the rows with specific value
print(df[df.Marks>50])

Output:

   Roll_no    Name  Gender  Marks Standard
1        2  Babita  Female     66   Fourth
2        3  Chetan    Male     76    Third
4        5    Ekta  Female     80    Third
# Performing filter operations
# Select the rows with specific value
print(df[df.Name.str.contains('k')])

Output:

   Roll_no      Name  Gender  Marks Standard
0        1  Abhishek    Male     50    Fifth
4        5      Ekta  Female     80    Third
# Define the set of values
lst = ['Female', 'Others']

# select the rows from specific set
# of values in a particular column
print(df[df.Gender.isin(lst)])

Output:

   Roll_no    Name  Gender  Marks Standard
1        2  Babita  Female     66   Fourth
4        5    Ekta  Female     80    Third

In this way, we can perform many more operations on our DataFrame.

Python Pandas Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.