I used to perform operations such as df.query("a > 10")
and they work fine.
However, I noticed that when I tried df.query("(a + b) / 2 > 10")
, it failed.
Fortunately, when I tried df.eval("(a + b) / 2 > 10")
, it works fine.
This leads me to the question what makes eval different from query and when should we use one vs the other?
The df.query
is to get the selected rows (where condition is fulfilled) and df.eval
is to get boolean output for each row.
e.g.
df123 = pd.DataFrame({'A': range(1, 6),
'B': range(10, 0, -2),
'C C': range(10, 5, -1)})
print(df123)
A B C C 0 1 10 10 1 2 8 9 2 3 6 8 3 4 4 7 4 5 2 6
df123.query('A > B')
A B C C 4 5 2 6
df123.eval('A > B')
0 False 1 False 2 False 3 False 4 True dtype: bool
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With