Pandas DataFrame eval() Method
Example
Calculate the sum of two columns:
import pandas as pd
data = {
"Women": [125, 230, 412],
"Men": [219, 185, 452]
}
df = pd.DataFrame(data)
print(df.eval("Women
+ Men"))
Try it Yourself »
Definition and Usage
The eval()
method evaluates the string
expression and returns the result.
You can refer to specific columns by specifying the column label(s).
Syntax
dataframe.eval(expr, inplace, kwargs)
Parameters
The
inplace
parameter is a
keyword argument.
Parameter | Value | Description |
---|---|---|
expr | Required. The string evaluate | |
inplace | True |
Optional, default False. Set to True if you want to perform the evaluation on the existing DataFrame instead of returning a new one. |
Return Value
A pandas object with the evaluated result.