search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | eval method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.eval(~) method evaluates an operation, which is encoded as a string, on the DataFrame's columns. This method does not support operations on specific rows and values.

WARNING

If you call eval(~) on a string given to you by someone else, there is the possibility that malicious code may run.

This does not mean that using eval(~) is bad practise - in fact, eval(~) is known to outperform other Pandas methods and so its use is encouraged. Just make sure to perform validation on the strings provided by others.

Parameters

1. exprlink | string | optional

The expression to evaluate.

2. inplace | boolean | optional

  • If True, then the source DataFrame will be directly modified.

  • If False, then a new DataFrame will be returned and the source DataFrame will be kept intact.

By default, inplace=False.

Return Value

The evaluated results - the type can vary depending on the result. If inplace=True, then nothing is returned as the source DataFrame is directly modified.

Examples

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,4],"B":[5,6],"C":[7,8]})
df
A B C
0 3 5 7
1 4 6 8

Basic usage

To compute the sum of each row:

df.eval("A + B + C")
0 15
1 18
dtype: int64

The return type here is a Series. Notice how we directly referred to the columns using their label, which is convenient because we typically have to refer to columns like df["A"].

Adding a new column

To add a new column:

df.eval("D = A + B + C")
A B C D
0 3 5 7 15
1 4 6 8 18

Since the inplace parameter is set to False by default, the source DataFrame is kept intact. To modify the source DataFrame directly without having to create a new DataFrame, set inplace=True:

df.eval("D = A + B + C", inplace=True)
df
A B C D
0 3 5 7 15
1 4 6 8 18

We show df here again for your reference:

df
A B C
0 3 5 7
1 4 6 8

Adding multiple columns

To add multiple columns at once:

df.eval(
"""
D = A + B
E = A + D
"""
)
A B C D E
0 3 5 7 8 11
1 4 6 8 10 14

Notice how we used column D, which originally wasn't in df, to create column E.

Using local variables

We can also access local variables defined outside eval(~) by using the @ syntax:

s = pd.Series([100,200])
df.eval("A + B + C + @s")
0 115
1 218
dtype: int64
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!