pandas.eval evaluates expressions involving pandas objects. With DataFrame.eval, column names can appear directly in the expression, making arithmetic, Boolean conditions, and assignments compact and readable. section 12.8
Use DataFrame.eval for several related tasks:
- Evaluate arithmetic such as (a + b) * 3 and return the resulting Series. section 12.8
- Create Boolean masks such as a > 100 & b < 700 for subsequent indexing. section 12.8
- Assign calculated columns, either one at a time or through multiple newline-separated assignments. section 12.8
- Use pd.eval with an explicit DataFrame target when evaluating expressions outside the DataFrame method. section 12.8
Filter parking-ticket data
After renaming awkward column headers to state, ptype, color, feet, and make, compare equivalent filters: df.loc[((df['state'] == 'NY') & (df['ptype'] == 'PAS'))], df.query('state == "NY" & ptype == "PAS"'), and df.loc[df.eval('state == "NY" & ptype == "PAS"')]. Extend the expression with color == "WHITE", feet > 1, and make == "TOYOT". section 12.10 section 12.9
df.eval('(a + b)* 3')
Inspect a compact arithmetic expression evaluated against DataFrame columns. section 12.8
The main practical advantage appears with large, complex conditions: evaluating the expression can avoid constructing multiple temporary Boolean Series. Measure rather than assume, because loc, query, isin, and eval may perform differently for a particular query. section 12.8 section 12.10
