Sunday 19 November 2023

Data Range Detective: Uncovering Relevant Data with Pandas between()

Pandas ‘between’ filter used to filter the values within in a specified range.

Let’s experiment with below data set.

 

       Name  Age       City  Gender  Rating
0   Krishna   34  Bangalore    Male      81
1     Sailu   35  Hyderabad  Female      76
2      Joel   29  Hyderabad    Male      67
3     Chamu   35    Chennai  Female     100
4  Jitendra   52  Bangalore    Male      87
5       Raj   34    Chennai    Male      89

 

Let’s try to get the Persons details whose Rating is in between 81 and 89, where 81 and 89 are inclusive.

 

We can solve the above problem using Logical & operator.

rating_greater_or_equal_81 = (df['Rating'] >= 81)
rating_less_than_or_equal_81 = (df['Rating'] <= 89)
final_condition = (rating_greater_or_equal_81 & rating_less_than_or_equal_81)
result = df[final_condition]

 

As you see above snippet, we defined to Boolean series ‘rating_greater_or_equal_81’, ‘rating_less_than_or_equal_81’ and applied logical & condition to that series.

 

We can achieve the same result using between operator like below.

final_condition = df['Rating'].between(81, 89)
result = df[final_condition]

Find the below working application.

 

between_filter_demo.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': [81, 76, 67, 100, 87, 89]}

df = pd.DataFrame(data)
print('Original DataFrame')
print(df)

print('\nPersons Rating is in between 81 and 89 using & operator')
rating_greater_or_equal_81 = (df['Rating'] >= 81)
rating_less_than_or_equal_81 = (df['Rating'] <= 89)
final_condition = (rating_greater_or_equal_81 & rating_less_than_or_equal_81)
result = df[final_condition]
print(result)

print('\nPersons Percentage is in between 81 and 89 using between operator')
final_condition = df['Rating'].between(81, 89)
result = df[final_condition]
print(result)

Output

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

Persons Percentage is in between 81 and 89 using | operator
       Name  Age       City Gender  Rating
0   Krishna   34  Bangalore   Male      81
4  Jitendra   52  Bangalore   Male      87
5       Raj   34    Chennai   Male      89

Persons Percentage is in between 81 and 89 using between operator
       Name  Age       City Gender  Rating
0   Krishna   34  Bangalore   Male      81
4  Jitendra   52  Bangalore   Male      87
5       Raj   34    Chennai   Male      89




Previous                                                 Next                                                 Home

No comments:

Post a Comment