gpt4 book ai didi

python - 获取混合索引 pandas Series 中前 K 个值的索引

转载 作者:太空宇宙 更新时间:2023-11-03 14:38:10 24 4
gpt4 key购买 nike

假设我有一个名为 z 的 pandas 系列:

index  items
155 14
2 555
1445 23
855 3

等等。正如您所看到的,指数好坏参半。如何在不更改当前索引的情况下获取前 K 行的索引列表?例如,对于 K=3,我想要这个:[155,2,1445]

谢谢。

最佳答案

您可以将ilocslice一起使用:

s.iloc[:3].index

或者使用head:

s.head(3).index
<小时/>
s
#index
#155 14
#2 555
#1445 23
#855 3
#Name: items, dtype: int64

s.iloc[:3].index.values
#array([ 155, 2, 1445], dtype=int64)

s.head(3).index.values
#array([ 155, 2, 1445], dtype=int64)

关于python - 获取混合索引 pandas Series 中前 K 个值的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46757698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com