Ale - 8 months ago 55

Python Question

I have 2 series.

The first one contains a list of numbers with an index counting 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])`

The second one only contains True values, but the index of the series is a subset of the first one.

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

I'd like to use B as boolean vector to get the A-values for the corresponding indexes, like:

`A[B]`

[...]

IndexingError: Unalignable boolean Series key provided

Unfortunately this raises an error.

Do I need to align them first?

Answer

Does

A[B.index.values]

work for your version of pandas? (I see we have different versions because now the Series name has to be hashable, so your code gave me an error)

Source (Stackoverflow)