Extract values in Pandas value_counts()

前端 未结 6 1297
余生分开走
余生分开走 2020-11-30 19:13

Say we have used pandas dataframe[column].value_counts() which outputs:

 apple   5 
 sausage 2
 banana  2
 cheese  1

How do yo

6条回答
  •  無奈伤痛
    2020-11-30 19:54

    The best way to extract the values is to just do the following

    json.loads(dataframe[column].value_counts().to_json())
    

    This returns a dictionary which you can use like any other dict. Using values or keys.

     {"apple": 5, "sausage": 2, "banana": 2, "cheese": 1}
    

提交回复
热议问题