Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas groupby to find percent True and False

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

Each site occurs 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 percent 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 do I use groupby to achieve this? I only know how to use groupby to find the mean for each site which won't help me here.

like image 454
pythanaconda Avatar asked May 18 '15 19:05

pythanaconda


People also ask

How do you get percentage in Groupby pandas?

You can calculate the percentage of total with the groupby of pandas DataFrame by using DataFrame. groupby() , DataFrame. agg() , DataFrame. transform() methods and DataFrame.

How do pandas calculate percentage difference?

Pandas DataFrame pct_change() Method The pct_change() method returns a DataFrame with the percentage difference between the values for each row and, by default, the previous row. Which row to compare with can be specified with the periods parameter.

How do you find the percentage of a total in Python?

A Percentage is calculated by the mathematical formula of dividing the value by the sum of all the values and then multiplying the sum by 100. This is also applicable in Pandas Dataframes. Here, the pre-defined sum() method of pandas series is used to compute the sum of all the values of a column.

Does Groupby preserve index?

The Groupby Rolling function does not preserve the original index and so when dates are the same within the Group, it is impossible to know which index value it pertains to from the original dataframe.


1 Answers

Something like this:

In [13]: g = df.groupby('SITE')['VALUE'].mean()
In [14]: g[g > 0.1]
Out[14]: 
SITE
Canada    0.5
USA       1.0
like image 84
Roman Pekar Avatar answered Oct 22 '22 20:10

Roman Pekar