count the frequency that a value occurs in a dataframe column

后端 未结 13 1865
耶瑟儿~
耶瑟儿~ 2020-11-22 03:29

I have a dataset

|category|
cat a
cat b
cat a

I\'d like to be able to return something like (showing unique values and frequency)



        
13条回答
  •  一向
    一向 (楼主)
    2020-11-22 04:12

    You can also do this with pandas by broadcasting your columns as categories first, e.g. dtype="category" e.g.

    cats = ['client', 'hotel', 'currency', 'ota', 'user_country']
    
    df[cats] = df[cats].astype('category')
    

    and then calling describe:

    df[cats].describe()
    

    This will give you a nice table of value counts and a bit more :):

        client  hotel   currency    ota user_country
    count   852845  852845  852845  852845  852845
    unique  2554    17477   132 14  219
    top 2198    13202   USD Hades   US
    freq    102562  8847    516500  242734  340992
    

提交回复
热议问题