Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count occurences of True/False in column of dataframe

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.

like image 275
Luca Giorgi Avatar asked Nov 21 '18 15:11

Luca Giorgi


People also ask

How do you count true and false values in Pandas?

Just sum the column for a count of the Trues. False is just a special case of 0 and True a special case of 1. The False count would be your row count minus that.

How do you count occurrences in a Dataframe column?

How do you Count the Number of Occurrences in a data frame? To count the number of occurrences in e.g. a column in a dataframe you can use Pandas value_counts() method. For example, if you type df['condition']. value_counts() you will get the frequency of each unique value in the column “condition”.

How do you count the number of true values in a data frame?

We can count by using the value_counts() method. This function is used to count the values present in the entire dataframe and also count values in a particular column.

How do you count Boolean values in a column Pandas?

Select the Dataframe column using the column name and subscript operator i.e. df['C']. It returns the column 'C' as a Series object of only bool values. After that, call the sum() function on this boolean Series object, and it will return the count of only True values in the Series/column.


1 Answers

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
like image 169
user3471881 Avatar answered Oct 24 '22 15:10

user3471881