dxb
dxb

Reputation: 283

Dataframe - get most frequent values and their count

There is a dataframe with a lot of records:

df = pd.DataFrame(columns=['id', 'product'])

To get the most frequent values:

df['product'].value_counts()[10].index.tolist()

What I would like to have is also the count of each value in front of it.

What is the way to do that?

Upvotes: 2

Views: 1183

Answers (1)

jezrael
jezrael

Reputation: 862661

I believe you need DataFrame with 2 columns filled by top10 values:

 df1 = df['product'].value_counts().iloc[:10].rename_axis('val').reset_index(name='count')

Upvotes: 2

Related Questions