nipy
nipy

Reputation: 5488

Percentage function on bool series in 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

Upvotes: 3

Views: 3282

Answers (2)

Eric D.
Eric D.

Reputation: 315

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

s.gt(5).mean()

Upvotes: 1

itscdo
itscdo

Reputation: 71

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

Upvotes: 6

Related Questions