search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
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
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Pandas DataFrame | eval method

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Basic and Descriptive Statistics
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

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
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!