Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.eval#

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 allowseval to run arbitrary code, which can make you vulnerable to codeinjection if you pass user input to this function.

Parameters:
exprstr

The expression string to evaluate.

inplacebool, default False

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

**kwargs

See the documentation foreval() for complete detailson the keyword arguments accepted byquery().

Returns:
ndarray, scalar, pandas object, or None

The result of the evaluation or None ifinplace=True.

See also

DataFrame.query

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

DataFrame.assign

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

eval

Evaluate a Python expression as a string using various backends.

Notes

For more details see the API documentation foreval().For detailed examples seeenhancing performance with eval.

Examples

>>>df=pd.DataFrame({'A':range(1,6),'B':range(10,0,-2)})>>>df   A   B0  1  101  2   82  3   63  4   44  5   2>>>df.eval('A + B')0    111    102     93     84     7dtype: int64

Assignment is allowed though by default the original DataFrame is notmodified.

>>>df.eval('C = A + B')   A   B   C0  1  10  111  2   8  102  3   6   93  4   4   84  5   2   7>>>df   A   B0  1  101  2   82  3   63  4   44  5   2

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

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

[8]ページ先頭

©2009-2025 Movatter.jp