percentage true in pandas series

Solutions on MaxInterview for percentage true in pandas series by the best coders in the world

showing results for - "percentage true in pandas series"
Emma
27 Nov 2018
1truths = pd.Series([True, False, False, True, True])
2
3# With only booleans, use that True => 1 and False => 0
4percent_true = truths.mean()       #==>  0.6
5percent_false = 1 - percent_true   #==>  0.4
6
7# Generalising to all values, but slower with just booleans
8percent_series = truths.value_counts(normalize=True)
9 #==> pd.Series (
10 #      True     0.6
11 #      False    0.4
12 #      dtype: float64
13 #    )
14  
15# First:   https://stackoverflow.com/a/52438477/
16# Second:  https://stackoverflow.com/a/52229939/