101

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

 apple   5 
 sausage 2
 banana  2
 cheese  1

How do you extract the values in the order same as shown above from max to min ?

e.g: [apple,sausage,banana,cheese]

shiva
  • 5,083
  • 5
  • 23
  • 42
JamesButterlips
  • 1,021
  • 2
  • 8
  • 5
  • 1
    Could you please provide a [MCVE](http://stackoverflow.com/help/mcve)? Did you use `dataframe.value_counts()` or `series.value_counts()`? What datatype do you have the output in? – albert Feb 20 '16 at 13:05
  • 4
    Note that the output of `value_counts()` is a series, so any series methods can be used, but often you'd just save it as is, depending on what you want to do with it later. – JohnE Feb 20 '16 at 15:51

5 Answers5

133

Try this:

dataframe[column].value_counts().index.tolist()
['apple', 'sausage', 'banana', 'cheese']
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
56
#!/usr/bin/env python

import pandas as pd

# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
                   (2, 'France'),
                   (3, 'Indonesia'),
                   (4, 'France'),
                   (5, 'France'),
                   (6, 'Germany'),
                   (7, 'UK'),
                   ],
                  columns=['groupid', 'country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])

# What you're looking for
values = df['country'].value_counts().keys().tolist()
counts = df['country'].value_counts().tolist()

Now, print(df['country'].value_counts()) gives:

France       3
Germany      2
UK           1
Indonesia    1

and print(values) gives:

['France', 'Germany', 'UK', 'Indonesia']

and print(counts) gives:

[3, 2, 1, 1]
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
28

If anyone missed it out in the comments, try this:

dataframe[column].value_counts().to_frame()
Sawant
  • 4,321
  • 1
  • 27
  • 30
10

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}
SummmerFort
  • 369
  • 3
  • 8
2

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']
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48