Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Write a Python program to count the total number of ages between 20 to 30 in a DataFrame
When working with Pandas DataFrames, you often need to count rows that meet specific conditions. In this case, we'll count how many ages fall between 20 and 30 using the between() method.
Sample DataFrame
Let's start with a DataFrame containing ID and Age columns ?
import pandas as pd
data = {'Id': [1, 2, 3, 4, 5], 'Age': [21, 23, 32, 35, 18]}
df = pd.DataFrame(data)
print(df)
Id Age 0 1 21 1 2 23 2 3 32 3 4 35 4 5 18
Using between() Method
The between() method checks if values fall within a specified range (inclusive by default) ?
import pandas as pd
data = {'Id': [1, 2, 3, 4, 5], 'Age': [21, 23, 32, 35, 18]}
df = pd.DataFrame(data)
# Filter ages between 20 and 30 (inclusive)
ages_in_range = df[df['Age'].between(20, 30)]
print("Ages between 20 to 30:")
print(ages_in_range)
# Count the total number
count = len(ages_in_range)
print(f"\nTotal number of ages between 20 to 30 is {count}.")
Ages between 20 to 30: Id Age 0 1 21 1 2 23 Total number of ages between 20 to 30 is 2.
Alternative Methods
Using Boolean Indexing
You can also use comparison operators for the same result ?
import pandas as pd
data = {'Id': [1, 2, 3, 4, 5], 'Age': [21, 23, 32, 35, 18]}
df = pd.DataFrame(data)
# Using boolean indexing
count = len(df[(df['Age'] >= 20) & (df['Age'] <= 30)])
print(f"Count using boolean indexing: {count}")
Count using boolean indexing: 2
Using sum() Method
Convert boolean conditions to integers and sum them ?
import pandas as pd
data = {'Id': [1, 2, 3, 4, 5], 'Age': [21, 23, 32, 35, 18]}
df = pd.DataFrame(data)
# Using sum on boolean series
count = df['Age'].between(20, 30).sum()
print(f"Count using sum method: {count}")
Count using sum method: 2
Comparison
| Method | Syntax | Best For |
|---|---|---|
between() |
df['Age'].between(20, 30) |
Range filtering (readable) |
| Boolean indexing | (df['Age'] >= 20) & (df['Age'] <= 30) |
Complex conditions |
sum() |
df['Age'].between(20, 30).sum() |
Direct counting |
Conclusion
Use between() for readable range filtering and sum() on the boolean result for direct counting. The between() method is inclusive by default, making it perfect for age range filtering.
Advertisements
