简体   繁体   中英

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?

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM