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.

Operator chaining

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.

Note

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let's start with creating a DataFrame first.

Create a DataFrame in Python

This program demonstrates how to create a DataFrame in Python?

# 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)

The output of the above program is:

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

Operations to filter pandas DataFrame by operator chaining

Exanple 1: Filtering rows having specific value

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

The output of the above program is:

   Roll_no     Name  Gender  Marks Standard
2        3   Chetan    Male     76    Third
3        4  Dheeraj    Male     45    Third
4        5     Ekta  Female     80    Third

Exanple 2: Filtering rows having value greater than the given value of a specific column

# Performing filter operations
# Select the rows with specific value
print(df[df.Marks>50])

The output of the above program is:

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

Exanple 3: Filtering rows having specific character in a given column

# Performing filter operations
# Select the rows with specific value
print(df[df.Name.str.contains('k')])

The output of the above program is:

   Roll_no      Name  Gender  Marks Standard
0        1  Abhishek    Male     50    Fifth
4        5      Ekta  Female     80    Third

Exanple 4: Filtering rows having specific values from the given list

# 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)])

The output of the above program is:

   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 »

Comments and Discussions!

Load comments ↻





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