gpt4 book ai didi

python - Pandas 在数组中查找子数组的索引

转载 作者:行者123 更新时间:2023-11-28 22:48:43 25 4
gpt4 key购买 nike

我有数组

a = numpy.array([1,3,5,7])
b = pandas.Series([1,2,3,4,5,6,7,8,9])

是否有快速命令来查找 b 中包含 a 中的值的所有匹配索引?例如

a in b = [0,2,4,6]

最佳答案

您可以使用 isin 来查找值在类数组对象中的位置(也适用于列表):

In [14]:

a = np.array([1,3,5,7])
b = pd.Series([1,2,3,4,5,6,7,8,9])
# call .index if you are just interested in the index values
b[b.isin(a)].index
Out[14]:
Int64Index([0, 2, 4, 6], dtype='int64')

如果不访问 .index 属性,您会得到一个返回的系列:

In [15]:

b[b.isin(a)]
Out[15]:
0 1
2 3
4 5
6 7
dtype: int64

关于python - Pandas 在数组中查找子数组的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24804396/

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