gpt4 book ai didi

pandas - 与列表不同,pandas 中的负索引会产生 KeyError

转载 作者:行者123 更新时间:2023-12-02 02:51:56 25 4
gpt4 key购买 nike

使用 Python 列表,您可以使用负索引进行切片。

a = [1,2,3,4,5,6,7,8,9]
print(a[-1])

将按预期打印 9

但是,

a = pd.Series([1,2,3,4,5,6,7,8,9])
print(a[-1])

给出KeyError:-1L

最佳答案

使用iloc通过位置而不是标签获取:

In [11]: a.iloc[-1]
Out[11]: 9

参见 selection section of the docs .

关于pandas - 与列表不同,pandas 中的负索引会产生 KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33504485/

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