我是熊猫的新手,我想过滤熊猫中的数据框,其中包含列表中的 top5 值。使用该代码从列表中获取 5 个值的最佳方法是什么?
我的代码:
cheese_top5 = cheese[cheese.year >= 2016]
Run Code Online (Sandbox Code Playgroud)
lux*_*ux7 11
您可以使用 pandas 方法nlargest:
df['column'].nlargest(n=5)
参考:https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.nlargest.html
我认为你正在寻找的是:
cheese.sort_values(by=['Name of column']).head(5)
Run Code Online (Sandbox Code Playgroud)
更重要的是,我们需要查看您的数据样本。
| 归档时间: |
|
| 查看次数: |
18021 次 |
| 最近记录: |