Extract values in Pandas value_counts()

前端 未结 6 1320
余生分开走
余生分开走 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:33

    First you have to sort the dataframe by the count column max to min if it's not sorted that way already. In your post, it is in the right order already but I will sort it anyways:

    dataframe.sort_index(by='count', ascending=[False])
        col     count
    0   apple   5
    1   sausage 2
    2   banana  2
    3   cheese  1 
    

    Then you can output the col column to a list:

    dataframe['col'].tolist()
    ['apple', 'sausage', 'banana', 'cheese']
    

提交回复
热议问题