Issue
Assume I have a DataFrame like this:
Num1 Num2
1 1 0
2 3 2
3 5 4
4 7 6
5 9 8
And I have a dictionary like this:
d = {
"Num1": 2,
"Num2": 5
}
I want to return the values from the DataFrame from each column that matches the key and the values greater than or equal to the value in the key.
My result should look like this:
Num1 Num2
1 nan nan
2 3 nan
3 5 nan
4 7 6
5 9 8
How might I do this?
Solution
The same idea of @db's answer using df.where
instead of df.mask
to keep the logic, i.e use ge
rather than lt
:
>>> df.where(df >= d) # or df.where(df.ge(d))
Num1 Num2
0 NaN NaN
1 3.0 NaN
2 5.0 NaN
3 7.0 6.0
4 9.0 8.0
Edit
To go further, if you want to use a different operator, you can change the input as follows:
filters = [ # col, op, val
('Num1', '>=', 2),
('Num2', '<', 5)
]
m = pd.concat([df.eval(f'{col} {op} {val}').rename(col)
for col, op, val in filters], axis=1)
out = df.where(m)
print(out)
# Output
Num1 Num2
0 NaN 0.0
1 3.0 2.0
2 5.0 4.0
3 7.0 NaN
4 9.0 NaN
Alternative:
filters = {
'Num1': '>= 2',
'Num2': '< 5'
}
m = pd.concat([df.eval(f'{col} {cond}').rename(col)
for col, cond in filters.items()], axis=1)
out = df.where(m)
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.