rookie rookie - 4 months ago 30
Python Question

Count items greater than a value in pandas groupby

I have the Yelp dataset and I want to count all reviews which have greater than 3 stars. I get the count of reviews by doing this:


Now I want to get the count of reviews which had more than 3 stars, so I tried this by taking inspiration from here:

reviews.groupby('business_id')['stars'].agg({'greater':lambda val: (val > 3).count()})

But this just gives me the count of all stars like before. I am not sure if this is the right way to do it? What am I doing incorrectly here. Does the lambda expression not go through each value of the stars column?

Okay I feel stupid. I should have used the sum function instead of count to get the value of elements greater than 3, like this:

reviews.groupby('business_id')['stars'].agg({'greater':lambda val: (val > 3).sum()})


You can try to do :

reviews[reviews['stars'] > 3].groupby('business_id')['stars'].count()