Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Percentage function on bool series in Pandas

Tags:

python

pandas

Using a function similar to this:

def percentage(part, whole):
    return 100 * float(part)/float(whole)

I can get the percentage calling the function:

percentage(215,413)

But how do I take a bool series and get the percentage of True and False?

s = pd.Series(np.random.randn(5) +5)

s > 5

0     True
1    False
2    False
3     True
4     True
dtype: bool

Desired output is percentage of True values in the series (60% in the example above). Do I need to map a function to get the output? Please show me the best way to do this. thanks

like image 200
nipy Avatar asked Dec 05 '25 08:12

nipy


2 Answers

Just calculate the mean of the boolean pandas series. Since True is mapped to 1 and False is mapped to 0, you will end up getting the percentage by simply calculating the mean.:

import pandas as pd, numpy as np
s = pd.Series(np.random.randn(5) +5)
(s > 5).mean()
0.59999999999999998
like image 100
itscdo Avatar answered Dec 07 '25 22:12

itscdo


The built in Series operators are another handy way to do this:

s.gt(5).mean()
like image 24
Eric D. Avatar answered Dec 07 '25 21:12

Eric D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!