Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: How to count the unique categories?

Tags:

python

pandas

I have a dataframe

df_input = pd.DataFrame(
        {
            "col_cate": ['A', 'A', 'B', 'B', 'A', 'A', 'B', 'B'],
            "target_bool": [True, False, True, False, True, False, True, False]
        }
    )

And I want to count the number of unique categories. So I am expecting the output to be like this

col_cate, target_bool, cnt
'A'     , True       , 2
'A'     , False      , 2
'B'     , True       , 2
'B'     , False      , 2

But df_input.group_by(["col_cate", "target_bool"]).count() gives

Empty DataFrame
Columns: []
Index: [(A, False), (A, True), (B, False), (B, True)]

But adding a dummy to the df_input works, like df_input["dummy"] = 1.

How do I get the group by count table without adding a dummy?

like image 526
xiaodai Avatar asked Dec 05 '25 04:12

xiaodai


1 Answers

df_input.groupby('col_cate')['target_bool'].value_counts()

col_cate  target_bool
A         False          2
          True           2
B         False          2
          True           2

then you can reset_index()

like image 123
Alice jinx Avatar answered Dec 06 '25 16:12

Alice jinx