pby.html)
  * [ Resampling ](../resampling.html)
  * [ Style ](../style.html)
  * [ Plotting ](../plotting.html)
  * [ Options and settings ](../options.html)
  * [ Extensions ](../extensions.html)
  * [ Testing ](../testing.html)
  * [ Missing values ](../missing_value.html)

  * [ __ ](../../index.html)
  * [ API reference ](../index.html)
  * [ DataFrame ](../frame.html)
  * pandas.DataF... 

#  pandas.DataFrame.eval  #

DataFrame.  eval  (  _ expr  _ , _ *  _ , _ inplace  =  False  _ , _ **
kwargs  _ )  [ [source]  ](https://github.com/pandas-
dev/pandas/blob/v2.2.2/pandas/core/frame.py#L4846-L4949) #

    

Evaluate a string describing operations on DataFrame columns.

Operates on columns only, not specific rows or elements. This allows  eval  to
run arbitrary code, which can make you vulnerable to code injection if you
pass user input to this function.

Parameters  :

    

**expr** str

    

The expression string to evaluate.

**inplace** bool, default False

    

If the expression contains an assignment, whether to perform the operation
inplace and mutate the existing DataFrame. Otherwise, a new DataFrame is
returned.

****kwargs**

    

See the documentation for [ ` eval()  ` ](pandas.eval.html#pandas.eval
"pandas.eval") for complete details on the keyword arguments accepted by [ `
query()  ` ](pandas.DataFrame.query.html#pandas.DataFrame.query
"pandas.DataFrame.query") .

Returns  :

    

ndarray, scalar, pandas object, or None

    

The result of the evaluation or None if ` inplace=True  ` .

See also

[ ` DataFrame.query  ` ](pandas.DataFrame.query.html#pandas.DataFrame.query
"pandas.DataFrame.query")

    

Evaluates a boolean expression to query the columns of a frame.

[ ` DataFrame.assign  ` ](pandas.DataFrame.assign.html#pandas.DataFrame.assign
"pandas.DataFrame.assign")

    

Can evaluate an expression or function to create new values for a column.

[ ` eval  ` ](pandas.eval.html#pandas.eval "pandas.eval")

    

Evaluate a Python expression as a string using various backends.

Notes

For more details see the API documentation for [ ` eval()  `
](pandas.eval.html#pandas.eval "pandas.eval") . For detailed examples see [
enhancing performance with eval
](../../user_guide/enhancingperf.html#enhancingperf-eval) .

Examples

    
    
    >>> df = pd.DataFrame({'A': range(1, 6), 'B': range(10, 0, -2)})
    >>> df
       A   B
    0  1  10
    1  2   8
    2  3   6
    3  4   4
    4  5   2
    >>> df.eval('A + B')
    0    11
    1    10
    2     9
    3     8
    4     7
    dtype: int64
    

Assignment is allowed though by default the original DataFrame is not
modified.

    
    
    >>> df.eval('C = A + B')
       A   B   C
    0  1  10  11
    1  2   8  10
    2  3   6   9
    3  4   4   8
    4  5   2   7
    >>> df
       A   B
    0  1  10
    1  2   8
    2  3   6
    3  4   4
    4  5   2
    

Multiple columns can be assigned to using multi-line expressions:

    
    
    >>> df.eval(
    ...     '''
    ... C = A + B
    ... D = A - B
    ... '''
    ... )
       A   B   C  D
    0  1  10  11 -9
    1  2   8  10 -6
    2  3   6   9 -3
    3  4   4   8  0
    4  5   2   7  3
    

[ __ previous  pandas.DataFrame.diff  ](pandas.DataFrame.diff.html "previous
page") [ next  pandas.DataFrame.kurt  __ ](pandas.DataFrame.kurt.html "next
page")

__ On this page

  * ` DataFrame.eval()  `

[ __ Show Source ](../../_sources/reference/api/pandas.DataFrame.eval.rst.txt)

© 2024, pandas via [ NumFOCUS, Inc. ](https://numfocus.org) Hosted by [
OVHcloud ](https://www.ovhcloud.com) .

Created using [ Sphinx ](https://www.sphinx-doc.org/) 7.2.6.  

Built with the [ PyData Sphinx Theme ](https://pydata-sphinx-
theme.readthedocs.io/en/stable/index.html) 0.14.4.