python - 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')
推荐阅读
- r - 基于两列在R中成对排列行
- python - 如何从 3D numpy meshgrid 中提取 2D 平面
- c# - 不要在外部条件下序列化字段C#
- r - R:使用方法中的错误(“tbl_vars”)
- python - 与 Exasol 的 Python 连接 - 导出到 pandas
- hyperledger-fabric - 使用事务预处理器发布身份
- php - 如果未选中,如何传递复选框值 0,如果使用数组 laravel 选中则传递 1
- reactjs - 当容器中有图像/文件时,scrollIntoView 不会滚动到底部
- javascript - 多动态反应形式Angular 5嵌套输入字段
- python - Python - 在双引号内打印整数变量