Pandas groupby to find the percentage of True and False

I have a column of sites: ['Canada', 'USA', 'China' ....]

Each site appears many times in the SITE column, and next to each instance is a true or false value.

INDEX | VALUE | SITE

0     | True  | Canada
1     | False | Canada
2     | True  | USA
3     | True  | USA

And it goes on.

Goal 1: I want to find for each site what percentage of the VALUE column is True.

Goal 2: I want to return a list of sites where% True in the VALUE column is greater than 10%.

How to use groupby to achieve this? I only know how to use groupby to find the average value for each site, which will not help me here.

+4
source share
1 answer

Something like that:

In [13]: g = df.groupby('SITE')['VALUE'].mean()
In [14]: g[g > 0.1]
Out[14]: 
SITE
Canada    0.5
USA       1.0
+5
source

All Articles