Evaluate mathematical expressions efficiently on Pandas DataFrames using the df.eval() method. Perform high-performance column computations, assign new columns inplace, and reference local Python variables using the @ prefix.
df.eval() method evaluates Python expression strings using underlying engine optimizations (such as numexpr when installed). It accelerates large numerical computations on DataFrame columns while keeping code clean and readable.import pandas as pd
df = pd.DataFrame({
"ITEM": ["Apple", "Banana", "Cherry", "Date"],
"QTY": [10, 25, 15, 30],
"PRICE": [20.0, 5.0, 50.0, 12.0]
})
print(df)
assert df.shape == (4, 3)
Expected output
ITEM QTY PRICE
0 Apple 10 20.0
1 Banana 25 5.0
2 Cherry 15 50.0
3 Date 30 12.0
df.eval() to compute operations across columns without explicit bracket indexers.res = df.eval("QTY * PRICE")
print(res)
assert res.iloc[0] == 200.0
Expected output
0 200.0
1 125.0
2 750.0
3 360.0
dtype: float64
inplace=True to create and assign a new column directly inside the existing DataFrame.df.eval("TOTAL = QTY * PRICE", inplace=True)
print(df)
assert "TOTAL" in df.columns and df.loc[0, "TOTAL"] == 200.0
Expected output
ITEM QTY PRICE TOTAL
0 Apple 10 20.0 200.0
1 Banana 25 5.0 125.0
2 Cherry 15 50.0 750.0
3 Date 30 12.0 360.0
@ inside your expression string to combine external parameters with DataFrame columns.tax_rate = 0.1
df.eval("TAX = TOTAL * @tax_rate", inplace=True)
print(df)
assert df.loc[0, "TAX"] == 20.0
Expected output
ITEM QTY PRICE TOTAL TAX
0 Apple 10 20.0 200.0 20.0
1 Banana 25 5.0 125.0 12.5
2 Cherry 15 50.0 750.0 75.0
3 Date 30 12.0 360.0 36.0
PRICE = [100, 200, 300] and DISCOUNT = [10, 20, 30]. Use df.eval() to compute a new column NET = PRICE - DISCOUNT and verify that the first row net equals 90.eval().ex_df = pd.DataFrame({"PRICE": [100, 200, 300], "DISCOUNT": [10, 20, 30]})
ex_df.eval("NET = PRICE - DISCOUNT", inplace=True)
print(ex_df)
assert ex_df.loc[0, "NET"] == 90
Expected output
PRICE DISCOUNT NET
0 100 10 90
1 200 20 180
2 300 30 270
Practice eval() examples interactively directly in your browser:
Reference: Pandas eval() documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.