Pandas DataFrame | eval method
Start your free 7-days trial now!
Pandas DataFrame.eval(~)
method evaluates an operation, which is encoded as a string, on the DataFrame's columns. This method does not support operations on specific rows and values.
If you call eval(~)
on a string given to you by someone else, there is the possibility that malicious code may run.
This does not mean that using eval(~)
is bad practise - in fact, eval(~)
is known to outperform other Pandas methods and so its use is encouraged. Just make sure to perform validation on the strings provided by others.
Parameters
1. expr
link | string
| optional
The expression to evaluate.
2. inplace
| boolean
| optional
If
True
, then the source DataFrame will be directly modified.If
False
, then a new DataFrame will be returned and the source DataFrame will be kept intact.
By default, inplace=False
.
Return Value
The evaluated results - the type can vary depending on the result. If inplace=True
, then nothing is returned as the source DataFrame is directly modified.
Examples
Consider the following DataFrame:
df = pd.DataFrame({"A":[3,4],"B":[5,6],"C":[7,8]})df
A B C0 3 5 71 4 6 8
Basic usage
To compute the sum of each row:
df.eval("A + B + C")
0 151 18dtype: int64
The return type here is a Series
. Notice how we directly referred to the columns using their label, which is convenient because we typically have to refer to columns like df["A"]
.
Adding a new column
To add a new column:
df.eval("D = A + B + C")
A B C D0 3 5 7 151 4 6 8 18
Since the inplace
parameter is set to False
by default, the source DataFrame is kept intact. To modify the source DataFrame directly without having to create a new DataFrame, set inplace=True
:
df.eval("D = A + B + C", inplace=True)df
A B C D0 3 5 7 151 4 6 8 18
We show df
here again for your reference:
df
A B C0 3 5 71 4 6 8
Adding multiple columns
To add multiple columns at once:
df.eval( """ D = A + B E = A + D """)
A B C D E0 3 5 7 8 111 4 6 8 10 14
Notice how we used column D
, which originally wasn't in df
, to create column E
.
Using local variables
We can also access local variables defined outside eval(~)
by using the @
syntax:
s = pd.Series([100,200])df.eval("A + B + C + @s")
0 1151 218dtype: int64