如何从熊猫数据框中获取前 5 个值?

You*_*sen 0 python pandas

我是熊猫的新手,我想过滤熊猫中的数据框,其中包含列表中的 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


Mar*_*ark 7

我认为你正在寻找的是:

cheese.sort_values(by=['Name of column']).head(5)
Run Code Online (Sandbox Code Playgroud)

更重要的是,我们需要查看您的数据样本。