Q:

How to filter pandas DataFrame by operator chaining?

belongs to collection: Python Pandas Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python Pandas: Conditional creation of a series/Da... >>
<< How to group DataFrame rows into list in pandas gr...