count the frequency that a value occurs in a dataf

2019-01-01 00:37发布

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)

category | freq |
cat a       2
cat b       1

标签: python pandas
14条回答
大哥的爱人
2楼-- · 2019-01-01 01:05
df.category.value_counts()

This short little line of code will give you the output you want.

查看更多
明月照影归
3楼-- · 2019-01-01 01:05
df.apply(pd.value_counts).fillna(0)

value_counts - Returns object containing counts of unique values

apply - count frequency in every column. If you set axis=1, you get frequncy in every row

fillna(0) - make output more fancy. Changed NaN to 0

查看更多
登录 后发表回答