@@ -4604,6 +4604,14 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
4604
4604
>>> df[df.B == df["C C"]]
4605
4605
A B C C
4606
4606
0 1 10 10
4607
+
4608
+ Using local variable:
4609
+
4610
+ >>> local_var = 2
4611
+ >>> df.query("A <= @local_var")
4612
+ A B C C
4613
+ 0 1 10 10
4614
+ 1 2 8 9
4607
4615
"""
4608
4616
inplace = validate_bool_kwarg (inplace , "inplace" )
4609
4617
if not isinstance (expr , str ):
@@ -4644,6 +4652,20 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:
4644
4652
----------
4645
4653
expr : str
4646
4654
The expression string to evaluate.
4655
+
4656
+ You can refer to variables
4657
+ in the environment by prefixing them with an '@' character like
4658
+ ``@a + b``.
4659
+
4660
+ You can refer to column names that are not valid Python variable names
4661
+ by surrounding them in backticks. Thus, column names containing spaces
4662
+ or punctuations (besides underscores) or starting with digits must be
4663
+ surrounded by backticks. (For example, a column named "Area (cm^2)" would
4664
+ be referenced as ```Area (cm^2)```). Column names which are Python keywords
4665
+ (like "list", "for", "import", etc) cannot be used.
4666
+
4667
+ For example, if one of your columns is called ``a a`` and you want
4668
+ to sum it with ``b``, your query should be ```a a` + b``.
4647
4669
inplace : bool, default False
4648
4670
If the expression contains an assignment, whether to perform the
4649
4671
operation inplace and mutate the existing DataFrame. Otherwise,
@@ -4723,6 +4745,17 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:
4723
4745
2 3 6 9 -3
4724
4746
3 4 4 8 0
4725
4747
4 5 2 7 3
4748
+
4749
+ Local variables shall be explicitely referenced using ``@``
4750
+ character in front of the name:
4751
+
4752
+ >>> local_var = 2
4753
+ >>> df.eval("@local_var * A")
4754
+ 0 2
4755
+ 1 4
4756
+ 2 6
4757
+ 3 8
4758
+ 4 10
4726
4759
"""
4727
4760
from pandas .core .computation .eval import eval as _eval
4728
4761
0 commit comments