🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.eval()

AI & DATA SCIENCE // df-eval

df.eval() evaluates a string expression against a DataFrame's columns, referencing column names directly as variables, similar to query() but for computing new values rather than filtering rows.

Syntax

df.eval(expr)

Deep Dive Course

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.

editor.html
import pandas as pd

df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
df.eval("total = price * quantity", inplace=True)
print(df)
localhost:3000

2Practical Example

Here is a real-world application of df.eval() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
result = df.eval("a + b")
print(result)
localhost:3000

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.

editor.html
import pandas as pd

df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
df.eval("total = price * quantity", inplace=True)
print(df)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
df.eval("total = price * quantity", inplace=True)
print(df)
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
result = df.eval("a + b")
print(result)

Best Practices

  • 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
  • Pass inplace=True to eval() when assigning the computed result as a new column directly, rather than capturing and reassigning the return value
  • 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

Interview Question

What's the difference in purpose between df.query() and df.eval(), given both use the same kind of string-expression syntax referencing column names?

Hint: Think about what each one is designed to produce as its result.

query() is specifically designed to evaluate a boolean expression and use it to filter the DataFrame's rows, returning a smaller DataFrame containing only the matching rows. eval() instead evaluates an arbitrary expression, not necessarily a boolean one, and returns the computed result directly, or assigns it as a new column when the expression includes an assignment and inplace=True is passed. They share the same underlying string-expression parsing engine and column-name-as-variable convention, but query() is for filtering, while eval() is for computing values.

Exercises

MediumPractice using df.eval() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
df.eval("total = price * quantity", inplace=True)
print(df)

Frequently Asked Questions

What's the difference in purpose between df.query() and df.eval(), given both use the same kind of string-expression syntax referencing column names?

query() is specifically designed to evaluate a boolean expression and use it to filter the DataFrame's rows, returning a smaller DataFrame containing only the matching rows. eval() instead evaluates an arbitrary expression, not necessarily a boolean one, and returns the computed result directly, or assigns it as a new column when the expression includes an assignment and inplace=True is passed. They share the same underlying string-expression parsing engine and column-name-as-variable convention, but query() is for filtering, while eval() is for computing values.

Related Functions

df-querydf-assignnp-add