Skip to content
Advertisement

Efficient chaining of boolean indexers in pandas DataFrames

I am trying to very efficiently chain a variable amount of boolean pandas Series, to be used as a filter on a DataFrame through boolean indexing.

Normally when dealing with multiple boolean conditions, one chains them like this

JavaScript

but this becomes a problem with a variable amount of conditions.

JavaScript

I have tried out some possible solutions, but I am convinced it can be done more efficiently.

Option 1
Loop over the indexers and apply consecutively.

JavaScript

Option 2
Put into a DataFrame and calculate the row product.

JavaScript

Option 3
Use numpy.product (like in this answer) and create a new Series out of the result.

JavaScript

All three solutions are somewhat inefficient because they rely on looping or force you to create a new object (which can be slow if repeated many times).

Can it be done more efficiently or is this it?

Advertisement

Answer

Use np.logical_and:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement