pandas – filter で条件で行を抽出する方法

pandas – filter で条件で行を抽出する方法





pandas.eval は、

pandas.eval(expr, parser='pandas', engine: Union[str, NoneType] = None, truediv=<object object at 0x7f5374a06320>, local_dict=None, global_dict=None, resolvers=(), level=0, target=None, inplace=False)
名前 デフォルト値
expr str
The expression to evaluate. This string cannot contain any Pythonstatements,only Python expressions.
parser {‘pandas’, ‘python’} ‘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’} ‘numexpr’
The engine used to evaluate the expression. Supported engines areNone : tries to use numexpr, falls back to python’numexpr’: This default engine evaluates pandas objects usingnumexpr for large speed ups in complex expressionswith large frames.’python’: Performs operations as if you had eval’d in toplevel python. This engine is generally not that useful.More backends may be available in the future.
truediv bool, optional
Whether to use true division, like in Python >= 3.deprecated:: 1.0.0
local_dict dict or None, optional
A dictionary of local variables, taken from locals() by default.
global_dict dict or None, optional
A dictionary of global variables, taken from globals() by default.
resolvers list 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 and DataFrame.columnsvariables that refer to their respective DataFrameinstance attributes.
level int, optional
The number of prior stack frames to traverse and add to the currentscope. Most users will not need to change this parameter.
target object, optional None
This is the target object for assignment. It is used when there isvariable assignment in the expression. If so, then target mustsupport item assignment with string keys, and if a copy is beingreturned, it must also support .copy().
inplace bool False
If target is provided, and the expression mutates target, whetherto modify target inplace. Otherwise, return a copy of target withthe mutation.

名前 説明
ndarray, numeric scalar, DataFrame, Series

名前 説明
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 + 1 a + 2 Here, there are expressions on different lines, making it multiline, but the last line has no variable assigned to the output of a + 2. inplace=True, but the expression is missing item assignment. Item assignment is provided, but the target does not support string item assignment. Item assignment is provided and inplace=False, but the target does not support the .copy() method


In [1]:
import pandas as pd
from IPython.display import display

df = pd.DataFrame({"A": range(1, 6), "B": range(10, 0, -2)})
0 1 10
1 2 8
2 3 6
3 4 4
4 5 2
In [2]:
print(df.eval("A + B"))
0    11
1    10
2     9
3     8
4     7
dtype: int64
In [3]:
print(df.eval("C = A ** 2"))
   A   B   C
0  1  10   1
1  2   8   4
2  3   6   9
3  4   4  16
4  5   2  25
In [4]:
df.eval("C = A ** 2", inplace=True)
0 1 10 1
1 2 8 4
2 3 6 9
3 4 4 16
4 5 2 25


pandas.DataFrame.eval は、

DataFrame.eval(self, expr, inplace=False, **kwargs)
名前 デフォルト値
expr str
The expression string to evaluate.
inplace bool False
If the expression contains an assignment, whether to perform theoperation inplace and mutate the existing DataFrame. Otherwise,a new DataFrame is returned.
See the documentation for eval() for complete detailson the keyword arguments accepted byquery().

名前 説明
ndarray, scalar, or pandas object The result of the evaluation.