Skip to content
Advertisement

Check values greater than int with condition Pandas

Say, I have this dataframe:

t = pd.DataFrame(data={"Pos": [1, 2, 3], "A": [10, 2, 90], "B":[90, 98, 10]})

I would like to know which of the (values in A and B >= 20) but only if (Pos = 1 or 3).

The result should look like this:

JavaScript

I can find the values >= 20 using t[["A", "B"]].ge(20) but I cannot seem to find a way to add the condition based on Pos.

Would a kind soul be able to help?

Advertisement

Answer

You can use a mask:

JavaScript

Alternative with numpy.logical_or and boolean inversion ~:

JavaScript

output:

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