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:
"A" "B"
False True
True True
True False
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?
CodePudding user response:
You can use a mask:
t[['A', 'B']].ge(20).where(t['Pos'].isin([1, 3]), True)
Alternative with numpy.logical_or
and boolean inversion ~
:
np.logical_or(t[["A", "B"]].ge(20), ~t['Pos'].isin([1, 3]).values[:,None])
output:
A B
0 False True
1 True True
2 True False