Pandas: Data Manipulation - eval() function

eval() function

The eval() function is used to concatenate pandas objects along a particular axis with optional set logic along the other axes.


pandas.eval(expr, parser='pandas', engine=None, truediv=True, local_dict=None, global_dict=None, resolvers=(), level=0, target=None, inplace=False

Arithmetic operations +, -, *, /, **, % are supported along with the the boolean operations | (or), & (and), and ~ (not).


Name Description Type / Default Value Required / Optional

The expression to evaluate. This string cannot contain any Python statements, only Python expressions.

str or unicode Required
parser The parser to use to construct the syntax tree from the expression. The default of 'pandas' parses code slightly different than standard Python. Alternatively, you can parse an expression using the 'python' parser to retain strict Python semantics. See the enhancing performance documentation for more details. string
Default Value: ‘pandas’, {‘pandas’, ‘python’}
engine  The engine used to evaluate the expression. Supported engines are
  • None : tries to use numexpr, falls back to python
  • 'numexpr': This default engine evaluates pandas objects using
    numexpr for large speed ups in complex expressions with large frames.
  • 'python': Performs operations as if you had eval’d in top
    level python. This engine is generally not that useful.

More backends may be available in the future.

string or None
Default Value: ‘numexpr’, {‘python’, ‘numexpr’}
truediv  Whether to use true division, like in Python >= 3 bool Optional
local_dict A dictionary of local variables, taken from locals() by default. dict or None Optional
global_dict A dictionary of global variables, taken from globals() by default. dict or None Optional
resolvers A list of objects implementing the __getitem__ special method that you can use to inject an additional collection of namespaces to use for variable lookup. For example, this is used in the query() method to inject the DataFrame.index and DataFrame.columns variables that refer to their respective DataFrame instance attributes.  list of dict-like or None Optional
level The number of prior stack frames to traverse and add to the current scope. Most users will not need to change this parameter. int Optional
target This is the target object for assignment. It is used when there is variable assignment in the expression. If so, then target must support item assignment with string keys, and if a copy is being returned, it must also support .copy(). object
Default Value: None
inplace If target is provided, and the expression mutates target, whether to modify target inplace. Otherwise, return a copy of target with the mutation. bool
Default Value: False

Returns: ndarray, numeric scalar, DataFrame, Series


There are many instances where an error can be raised:

  • target=None, but the expression is multiline.
  • The expression is multiline, but not all them have item assignment. An example of such an arrangement is this:
    a = b + 1 a + 2
  • inplace=True, but the expression is missing item assignment.
  • Item assignment is provided, but the target does not support string item assignment.
  • Item assignment is provided and inplace=False, but the target does not support the .copy() method


The dtype of any objects involved in an arithmetic % operation are recursively cast to float64.


Download the above Notebook from here.

Previous: interval_range() function
Next: Python pandas tutorials