Answers for "percentage true in pandas series"

0

percentage true in pandas series

truths = pd.Series([True, False, False, True, True])

# With only booleans, use that True => 1 and False => 0
percent_true = truths.mean()       #==>  0.6
percent_false = 1 - percent_true   #==>  0.4

# Generalising to all values, but slower with just booleans
percent_series = truths.value_counts(normalize=True)
 #==> pd.Series (
 #      True     0.6
 #      False    0.4
 #      dtype: float64
 #    )
  
# First:   https://stackoverflow.com/a/52438477/
# Second:  https://stackoverflow.com/a/52229939/
Posted by: Guest on November-26-2020

Python Answers by Framework

Browse Popular Code Answers by Language