Count occurences of True/False in column of dataframe

Luca Giorgi picture Luca Giorgi · Nov 21, 2018 · Viewed 19k times · Source

Is there a way to count the number of occurrences of boolean values in a column without having to loop through the DataFrame?

Doing something like

df[df["boolean_column"]==False]["boolean_column"].sum()

Will not work because False has a value of 0, hence a sum of zeroes will always return 0.

Obviously you could count the occurrences by looping over the column and checking, but I wanted to know if there's a pythonic way of doing this.

Answer

user3471881 picture user3471881 · Nov 21, 2018

Use pd.Series.value_counts():

>> df = pd.DataFrame({'boolean_column': [True, False, True, False, True]})
>> df['boolean_column'].value_counts()
True     3
False    2
Name: boolean_column, dtype: int64

If you want to count False and True separately you can use pd.Series.sum() + ~:

>> df['boolean_column'].values.sum()  # True
3
>> (~df['boolean_column']).values.sum() # False
2