DataFrame.eval(expr, inplace=False, **kwargs)
[source]
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. New in version 0.18.0.. kwargs : dict See the documentation for |
---|---|
Returns: |
ndarray, scalar, or pandas object The result of the evaluation. |
See also
DataFrame.query
DataFrame.assign
pandas.eval
For more details see the API documentation for eval()
. For detailed examples see enhancing performance with eval.
>>> 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
Use inplace=True
to modify the original DataFrame.
>>> df.eval('C = A + B', inplace=True) >>> df A B C 0 1 10 11 1 2 8 10 2 3 6 9 3 4 4 8 4 5 2 7
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
http://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DataFrame.eval.html