Extract values in Pandas value_counts() Extract values in Pandas value_counts() python python

Extract values in Pandas value_counts()


Try this:

dataframe[column].value_counts().index.tolist()['apple', 'sausage', 'banana', 'cheese']


#!/usr/bin/env pythonimport pandas as pd# Make example dataframedf = 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 forvalues = df['country'].value_counts().keys().tolist()counts = df['country'].value_counts().tolist()

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

France       3Germany      2UK           1Indonesia    1

and print(values) gives:

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

and print(counts) gives:

[3, 2, 1, 1]


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

dataframe[column].value_counts().to_frame()