Examples

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

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

In [4]:
df.eval('R = P + Q')
Out[4]:
P Q R
0 2 12 14
1 3 8 11
2 4 4 8
In [5]:
df
Out[5]:
P Q
0 2 12
1 3 8
2 4 4

Use inplace=True to modify the original DataFrame:

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