Where query() filters rows using a boolean string expression, eval() computes and returns a new value, typically to assign as a new column, using the same string-expression syntax referencing column names directly. For very large DataFrames, eval() can be noticeably faster than the equivalent expression written directly in Python, since it can avoid allocating some intermediate temporary arrays that a chained sequence of regular pandas operations would otherwise create.
1Understanding df.eval()
Where query() filters rows using a boolean string expression, eval() computes and returns a new value, typically to assign as a new column, using the same string-expression syntax referencing column names directly. For very large DataFrames, eval() can be noticeably faster than the equivalent expression written directly in Python, since it can avoid allocating some intermediate temporary arrays that a chained sequence of regular pandas operations would otherwise create.
Use df.eval('new_col = expr', inplace=True) to compute and assign a new column directly from a string expression, rather than the equivalent regular Python assignment syntax, when the extra performance on a large DataFrame is worth the different syntax.
import pandas as pd
df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
df.eval("total = price * quantity", inplace=True)
print(df)2Practical Example
Here is a real-world application of df.eval() showing how it is used in production Pandas code.
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
result = df.eval("a + b")
print(result)3Best Practices
Follow these guidelines when working with df.eval():
1. Use eval() for computing a new column from an expression, and query() for filtering rows with a boolean expression — they're complementary, string-syntax counterparts for different jobs
2. Pass inplace=True to eval() when assigning the computed result as a new column directly, rather than capturing and reassigning the return value
3. Reach for eval() specifically on large DataFrames where the performance benefit is measurable — for small data, the regular Python syntax is just as fast and often more familiar
Tip: Use df.eval('new_col = expr', inplace=True) to compute and assign a new column directly from a string expression, rather than the equivalent regular Python assignment syntax, when the extra performance on a large DataFrame is worth the different syntax.
import pandas as pd
df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
df.eval("total = price * quantity", inplace=True)
print(df)