gpt4 book ai didi

python - Pandas 选择未对齐的索引

转载 作者:行者123 更新时间:2023-11-28 21:45:00 25 4
gpt4 key购买 nike

我有 2 个系列。第一个包含一个数字列表,索引为 0..8。

A = pd.Series([2,3,4,6,5,4,7,6,5], name=['A'], index=[0,1,2,3,4,5,6,7,8])

第二个只包含 True 值,但该系列的索引是第一个的子集。

B = pd.Series([1, 1, 1, 1, 1], name=['B'], index=[0,2,4,7,8], dtype=bool)

我想使用 B 作为 bool 向量来获取相应索引的 A 值,例如:

A[B]

[...]
IndexingError: Unalignable boolean Series key provided

不幸的是,这会引发错误。

我需要先对齐它们吗?

最佳答案

A[B.index.values]

为您的 pandas 版本工作? (我看到我们有不同的版本,因为现在系列名称必须是可哈希的,所以你的代码给了我一个错误)

关于python - Pandas 选择未对齐的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40193255/

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