[英]Get the index of n maximum values in a column in dataframe
嘗試最大,
df.nlargest(4, 'a').reset_index()
輸出:
index a id
0 0 10 100
1 8 8 2
2 7 7 1
3 6 6 0
您可以對a
列進行排序
out = (df.sort_values('a', ascending=False).iloc[:4]
.sort_index(ascending=True)
.reset_index())
print(out)
index a id
0 0 10 100
1 6 6 0
2 7 7 1
3 8 8 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.