2

Suppose I have a Pandas Series:

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

a    1
b    2
c    3
dtype: int64

Comparing the index to a value returns a nice selector array:

foo.index == 'c'

array([False, False,  True], dtype=bool)

What is the expression for a selector array for 'a' and 'c' ([True, False, True])?

Not this:

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()

This is a simple example, but the true one is more complicated, and I want to select 10 or 15 items, so I'd like a concise format, ideally listing the elements I want to select by name.

I'm using pandas 0.23.4.

dfrankow
  • 20,191
  • 41
  • 152
  • 214

1 Answers1

3

You can use:

foo.index.isin(['a','b'])

which returns your selector array for a and b, you can arbitrarily change the list if different values are required.

FChm
  • 2,515
  • 1
  • 17
  • 37