count the frequency that a value occurs in a dataframe column

后端 未结 13 2101
耶瑟儿~
耶瑟儿~ 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:15

    your data:
    
    |category|
    cat a
    cat b
    cat a
    

    solution:

     df['freq'] = df.groupby('category')['category'].transform('count')
     df =  df.drop_duplicates()
    

提交回复
热议问题