Tuesday 14 November 2023

Combine the conditions using logical operators in a Pandas dataframe

Pandas support following logical operators.

a.   & for "and",

b.   | for "or",

c.    ~ for "not"

 

These operators are used to combine multiple conditional operators while filtering the data.

 

Example 1: Get all the persons whose Gender is "Male" and Rating is > 45

gender_male_boolean_series = (df['Gender'] == 'Male')
rating_is_greater_45 = (df['Rating'] > 45)
mask = (gender_male_boolean_series & rating_is_greater_45)
result = df[mask]

 

Above snippet generate below data set.

   Name  Age       City Gender  Rating
2  Joel   29  Hyderabad   Male      67
5   Raj   34    Chennai   Male      89

Example 2: Get all the persons whose Gender is "Male" or Rating is > 45

mask = (gender_male_boolean_series | rating_is_greater_45)
result = df[mask]

Above snippet generate below data set.

       Name  Age       City  Gender  Rating
0   Krishna   34  Bangalore    Male      39
2      Joel   29  Hyderabad    Male      67
3     Chamu   35    Chennai  Female     100
4  Jitendra   52  Bangalore    Male      41
5       Raj   34    Chennai    Male      89

Example 3: Get all the persons whose city is not Bangalore.

mask = ~(df['City'] == 'Bangalore')
result = df[mask]

Above snippet generate below dataset.

    Name  Age       City  Gender  Rating
1  Sailu   35  Hyderabad  Female      43
2   Joel   29  Hyderabad    Male      67
3  Chamu   35    Chennai  Female     100
5    Raj   34    Chennai    Male      89

Find the below working application.

 

filter_by_logical_operators.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Raj"],
        'Age': [34, 35, 29, 35, 52, 34],
        'City': ['Bangalore', 'Hyderabad', 'Hyderabad', 'Chennai', 'Bangalore', 'Chennai'],
        'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male'],
        'Rating': [39, 43, 67, 100, 41, 89]}
df = pd.DataFrame(data)
print('Original DataFrame')
print(df)

print('\nGet all the students whose Gender is "Male" and Rating is > 45')
gender_male_boolean_series = (df['Gender'] == 'Male')
rating_is_greater_45 = (df['Rating'] > 45)
mask = (gender_male_boolean_series & rating_is_greater_45)
result = df[mask]
print(result)

print('\nGet all the students whose Gender is "Male" or Rating is > 45')
mask = (gender_male_boolean_series | rating_is_greater_45)
result = df[mask]
print(result)

print('\nGet all the persons whose city is not Bangalore.')
mask = ~(df['City'] == 'Bangalore')
result = df[mask]
print(result)

Output

Original DataFrame
       Name  Age       City  Gender  Rating
0   Krishna   34  Bangalore    Male      39
1     Sailu   35  Hyderabad  Female      43
2      Joel   29  Hyderabad    Male      67
3     Chamu   35    Chennai  Female     100
4  Jitendra   52  Bangalore    Male      41
5       Raj   34    Chennai    Male      89

Get all the students whose Gender is "Male" and Rating is > 45
   Name  Age       City Gender  Rating
2  Joel   29  Hyderabad   Male      67
5   Raj   34    Chennai   Male      89

Get all the students whose Gender is "Male" or Rating is > 45
       Name  Age       City  Gender  Rating
0   Krishna   34  Bangalore    Male      39
2      Joel   29  Hyderabad    Male      67
3     Chamu   35    Chennai  Female     100
4  Jitendra   52  Bangalore    Male      41
5       Raj   34    Chennai    Male      89

Get all the persons whose city is not Bangalore.
    Name  Age       City  Gender  Rating
1  Sailu   35  Hyderabad  Female      43
2   Joel   29  Hyderabad    Male      67
3  Chamu   35    Chennai  Female     100
5    Raj   34    Chennai    Male      89

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment