Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.eval#

pandas.eval(expr,parser='pandas',engine=None,local_dict=None,global_dict=None,resolvers=(),level=0,target=None,inplace=False)[source]#

Evaluate a Python expression as a string using various backends.

The following arithmetic operations are supported:+,-,*,/,**,%,// (python engine only) along with the followingboolean operations:| (or),& (and), and~ (not).Additionally, the'pandas' parser allows the use ofand,or, andnot with the same semantics as thecorresponding bitwise operators.Series andDataFrame objects are supported and behave as they wouldwith plain ol’ Python evaluation.

Parameters:
exprstr

The expression to evaluate. This string cannot contain any Pythonstatements,only Pythonexpressions.

parser{‘pandas’, ‘python’}, default ‘pandas’

The parser to use to construct the syntax tree from the expression. Thedefault of'pandas' parses code slightly different than standardPython. Alternatively, you can parse an expression using the'python' parser to retain strict Python semantics. See theenhancing performance documentation formore details.

engine{‘python’, ‘numexpr’}, default ‘numexpr’

The engine used to evaluate the expression. Supported engines are

  • None : tries to usenumexpr, falls back topython

  • 'numexpr' : This default engine evaluates pandas objects usingnumexpr for large speed ups in complex expressions with large frames.

  • 'python' : Performs operations as if you hadeval’d in toplevel python. This engine is generally not that useful.

More backends may be available in the future.

local_dictdict or None, optional

A dictionary of local variables, taken from locals() by default.

global_dictdict or None, optional

A dictionary of global variables, taken from globals() by default.

resolverslist of dict-like or None, optional

A list of objects implementing the__getitem__ special method thatyou can use to inject an additional collection of namespaces to use forvariable lookup. For example, this is used in thequery() method to inject theDataFrame.index andDataFrame.columnsvariables that refer to their respectiveDataFrameinstance attributes.

levelint, optional

The number of prior stack frames to traverse and add to the currentscope. Most users willnot need to change this parameter.

targetobject, optional, default None

This is the target object for assignment. It is used when there isvariable assignment in the expression. If so, thentarget mustsupport item assignment with string keys, and if a copy is beingreturned, it must also support.copy().

inplacebool, default False

Iftarget is provided, and the expression mutatestarget, whetherto modifytarget inplace. Otherwise, return a copy oftarget withthe mutation.

Returns:
ndarray, numeric scalar, DataFrame, Series, or None

The completion value of evaluating the given code or None ifinplace=True.

Raises:
ValueError

There are many instances where such 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 + 1a + 2

    Here, there are expressions on different lines, making it multiline,but the last line has no variable assigned to the output ofa + 2.

  • inplace=True, but the expression is missing item assignment.

  • Item assignment is provided, but thetarget does not supportstring item assignment.

  • Item assignment is provided andinplace=False, but thetargetdoes not support the.copy() method

See also

DataFrame.query

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

DataFrame.eval

Evaluate a string describing operations on DataFrame columns.

Notes

Thedtype of any objects involved in an arithmetic% operation arerecursively cast tofloat64.

See theenhancing performance documentation formore details.

Examples

>>>df=pd.DataFrame({"animal":["dog","pig"],"age":[10,20]})>>>df  animal  age0    dog   101    pig   20

We can add a new column usingpd.eval:

>>>pd.eval("double_age = df.age * 2",target=df)  animal  age  double_age0    dog   10          201    pig   20          40
On this page

[8]ページ先頭

©2009-2025 Movatter.jp