pandas 1.4.2

NotesParametersReturnsBackRef
eval(self, expr: 'str', inplace: 'bool' = False, **kwargs)

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.

Notes

For more details see the API documentation for ~eval . For detailed examples see enhancing performance with eval <enhancingperf.eval> .

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 for complete details on the keyword arguments accepted by ~pandas.DataFrame.query .

Returns

ndarray, scalar, pandas object, or None

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

Evaluate a string describing operations on DataFrame columns.

See Also

DataFrame.assign

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

DataFrame.query

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

eval

Evaluate a Python expression as a string using various backends.

Examples

This example is valid syntax, but we were not able to check execution
>>> 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
This example is valid syntax, but we were not able to check execution
>>> 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.

This example is valid syntax, but we were not able to check execution
>>> 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
This example is valid syntax, but we were not able to check execution
>>> 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.

This example is valid syntax, but we were not able to check execution
>>> 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

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

This example is valid syntax, but we were not able to check execution
>>> 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
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.frame.DataFrame.query pandas.core.computation.eval.eval pandas.core.frame.DataFrame.eval

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#4126
type: <class 'function'>
Commit: