首页 > 解决方案 > 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?

标签: pythonpandasdataframecount

解决方案


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

推荐阅读