gpt4 book ai didi

pandas 从索引列中选择范围

转载 作者:行者123 更新时间:2023-12-03 09:00:37 25 4
gpt4 key购买 nike

我需要创建一个函数来选择索引范围(第一列)。

1880    Aachen  1   Valid   L5  21.0    Fell    50.77500    6.08333 (50.775000, 6.083330)
1951 Aarhus 2 Valid H6 720.0 Fell 56.18333 10.23333 (56.183330, 10.233330)
1952 Abee 6 Valid EH4 107000.0 Fell 54.21667 -113.00000 (54.216670, -113.000000)
1976 Acapulco 10 Valid Acapulcoite 1914.0 Fell 16.88333 -99.90000 (16.883330, -99.900000)
1902 Achiras 370 Valid L6 780.0 Fell -33.16667 -64.95000 (-33.166670, -64.950000)

我怎样才能做到这一点?

最佳答案

实际上,在撰写此答案时,您可以在 pandas 中使用 DataFrame 属性 loc。这是网上摘录的documentation :

Access a group of rows and columns by label(s) or a boolean array.

使用您的数据创建 DataFrame,如 Bill Armstrong 所做的那样,并且使用切片会产生以下结果,而无需开发新函数:

print(df.loc[1951:1976])

0 1 2 ... 6 7 8
1951 Aarhus 2 Valid ... 56.18333 10.23333 (56.18333, 10.23333)
1952 Abee 6 Valid ... 54.21667 -113.0 (54.21667, -113.0)
1976 Acapulco 10 Valid ... 16.88333 -99.9 (16.88333, -99.9)
[3 rows x 9 columns]

关于pandas 从索引列中选择范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50805446/

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