Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when should I use query vs eval on a pandas dataframe?

Tags:

python

pandas

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?

like image 302
user1559897 Avatar asked Sep 07 '18 19:09

user1559897


1 Answers

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!

like image 200
rishi jain Avatar answered Oct 19 '22 15:10

rishi jain