Jb_Eyd
Jb_Eyd

Reputation: 635

Pandas: quick way to tabulate True / False in a DF column

I am looking for a clever way to tabulate True / False values on DF column.

Suppose we have the following example:

    Array = np.array([[87, 70],[52, 47],[44, 97],[79, 36]])
    df_test = pd.DataFrame(Array, columns=['A', 'B'],index=[['Joe', 'Steve', 'Wes', 'Jim']])

If I want to know the number of person whose variable A is higher than 53

df_test["A"]>53
Joe       True
Steve    False
Wes      False
Jim       True
Name: Apple, dtype: bool

I am looking for a smart way to get the total number of True / False without selecting the data in the DF. In R it would be the table() function. The result would look like:

True: 2
False: 2

Does someone have an idea ?

Upvotes: 0

Views: 967

Answers (1)

Abbas
Abbas

Reputation: 4069

Here you go:

df_test.groupby(df_test['A']>53)['A'].count()

will return this:

A
False    2
True     2
Name: A, dtype: int64

Upvotes: 2

Related Questions