Examples

In [1]:
import numpy as np
import pandas as pd
In [7]:
df = pd.DataFrame({'P': range(1, 5), 'Q': range(8, 0, -2)})
df
Out[7]:
P Q
0 1 8
1 2 6
2 3 4
3 4 2
In [9]:
df.eval('P + Q')
Out[9]:
0    9
1    8
2    7
3    6
dtype: int64

Assignment is allowed though by default the original DataFrame is not modified.

In [10]:
df.eval('R = P + Q')
Out[10]:
P Q R
0 1 8 9
1 2 6 8
2 3 4 7
3 4 2 6
In [11]:
df
Out[11]:
P Q
0 1 8
1 2 6
2 3 4
3 4 2

Use inplace=True to modify the original DataFrame.

In [12]:
df.eval('R = P + Q', inplace=True)
df
Out[12]:
P Q R
0 1 8 9
1 2 6 8
2 3 4 7
3 4 2 6