Pandas 在一个系列中选择多个索引值

Pandas 在一个系列中选择多个索引值,pandas,Pandas,假设我有一个熊猫系列: import pandas as pd foo = pd.Series(data=[1,2,3], index=['a','b','c']) foo a 1 b 2 c 3 dtype: int64 将索引与值进行比较会返回一个很好的选择器数组: foo.index == 'c' array([False, False, True], dtype=bool) “a”和“c”([True,False,True])的选择器数组的表达式是什么 不是这

假设我有一个熊猫系列:

import pandas as pd
foo = pd.Series(data=[1,2,3], index=['a','b','c'])
foo

a    1
b    2
c    3
dtype: int64
将索引与值进行比较会返回一个很好的选择器数组:

foo.index == 'c'

array([False, False,  True], dtype=bool)
“a”和“c”([True,False,True])的选择器数组的表达式是什么

不是这个:

foo.index in ['a', 'c']

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
这是一个简单的例子,但真正的例子更复杂,我想选择10或15个项目,所以我想要一个简洁的格式,理想的情况是按名称列出我想要选择的元素

我正在使用pandas 0.23.4。

您可以使用:

foo.index.isin(['a','b'])
它返回
a
b
的选择器数组,如果需要不同的值,可以任意更改列表