Skip to content

Commit a72158f

Browse files
MarcoGorelliroberthdevries
authored andcommitted
add eval examples (pandas-dev#31955)
1 parent 0c84528 commit a72158f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pandas/core/computation/eval.py

+15
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,21 @@ def eval(
276276
277277
See the :ref:`enhancing performance <enhancingperf.eval>` documentation for
278278
more details.
279+
280+
Examples
281+
--------
282+
>>> df = pd.DataFrame({"animal": ["dog", "pig"], "age": [10, 20]})
283+
>>> df
284+
animal age
285+
0 dog 10
286+
1 pig 20
287+
288+
We can add a new column using ``pd.eval``:
289+
290+
>>> pd.eval("double_age = df.age * 2", target=df)
291+
animal age double_age
292+
0 dog 10 20
293+
1 pig 20 40
279294
"""
280295
inplace = validate_bool_kwarg(inplace, "inplace")
281296

pandas/core/frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -3319,6 +3319,21 @@ def eval(self, expr, inplace=False, **kwargs):
33193319
2 3 6 9
33203320
3 4 4 8
33213321
4 5 2 7
3322+
3323+
Multiple columns can be assigned to using multi-line expressions:
3324+
3325+
>>> df.eval(
3326+
... '''
3327+
... C = A + B
3328+
... D = A - B
3329+
... '''
3330+
... )
3331+
A B C D
3332+
0 1 10 11 -9
3333+
1 2 8 10 -6
3334+
2 3 6 9 -3
3335+
3 4 4 8 0
3336+
4 5 2 7 3
33223337
"""
33233338
from pandas.core.computation.eval import eval as _eval
33243339

0 commit comments

Comments
 (0)