Pandas groupby count with condition

When using the pandas library in Python, you can group data based on certain criteria and count the number of occurrences that satisfy a specific condition.

Here’s an example to illustrate how to use groupby and count in pandas:

import pandas as pd

# Create a sample DataFrame
data = {
    'Category': ['A', 'B', 'B', 'A', 'A', 'B'],
    'Value': [1, 2, 3, 4, 5, 6]
}
df = pd.DataFrame(data)

# Groupby 'Category' and count occurrences where 'Value' > 3
result = df[df['Value'] > 3].groupby('Category').size()
print(result)

In the above example, we create a DataFrame with two columns: ‘Category’ and ‘Value’. We then use groupby('Category') to group the data based on the ‘Category’ column. After that, we use the size() function to count the number of occurrences where the ‘Value’ is greater than 3.

The output would be:

Category
A    2
B    1
dtype: int64
    

This output shows that there are 2 occurrences where ‘Value’ > 3 in the ‘A’ category, and 1 occurrence in the ‘B’ category.

Leave a comment