首页 > 解决方案 > 如何提取数据框中列中前两个值的 ID?

问题描述

我有一个数据框,我需要从中获取特定列的前两个值的 ID(索引值)。

例子

two_highest_degree_id=karate.nlargest(2,'degree')

空手道是我的数据框,我需要列度数的前两个值的索引值。上面的脚本给了我以下结果:

      club     degree  cluster_coef
33  Officer      17      0.110294
0    Mr. Hi      16      0.150000

如何将索引值33和 &分配0给输出

标签: pythonpandasdataframe

解决方案


import pandas as pd
df = pd.DataFrame([10,9,8,7,6,50], columns = ['degree'])

df.nlargest(2,'degree').index
#op
Int64Index([5, 0], dtype='int64')

推荐阅读