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

Conditionally updating values of a DataFrame in Pandas

Pandas
chevron_right
Cookbooks
chevron_right
DataFrame Cookbooks
chevron_right
Data Manipulation Cookbook
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Consider the following DataFrame:

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

Conditionally updating all values

To update values that are larger than 3 in the entire DataFrame:

df[df > 3] = 10
df
A B
0 3 10
1 10 10

Explanation

Here, we're first creating a DataFrame of booleans based on our criteria:

df > 3
A B
0 False True
1 True True

True represents entries that match our criteria. Placing this mask into our df using [~] returns the references to the matched entries:

df[df > 3]
A B
0 NaN 5
1 4.0 6

We can then update the values using = like so:

df[df > 3] = 10
df
A B
0 3 10
1 10 10

Conditionally updating values for specific columns

Consider the same DataFrame we had before:

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

Instead of updating the values of the entire DataFrame, we can select the columns to conditionally update using the loc property:

df.loc[df["A"] > 3, "A"] = 10
df
A B
0 3 5
1 10 6

Here, we are updating values that are greater than 3 in column A.

Explanation

To break down the components of loc, here's the boolean mask we are passing in:

df["A"] > 3
0 False
1 True
Name: A, dtype: bool

This is a Series, where True indicates the entry that satisfied the criteria.

The trap here is that, if we just pass this mask directly into loc, we end up with the second row being updated:

df.loc[df["A"] > 3] = 10
df
A B
0 3 5
1 10 10

This is not what we want since we want to perform updates on column A only. To this end, we need to specify the columns like so:

df.loc[df["A"] > 3, "A"] = 10
df
A B
0 3 5
1 10 6

Conditionally updating values based on their value

Consider the following DataFrame:

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

All values in the DataFrame

To update values based on their value, use the applymap(~) method like so:

df = df.applymap(lambda val: 2*val if val > 3 else val)
df
A B
0 3 10
1 8 12

Here, we're doubling values that are greater than 3. This approach gives you the flexibility of setting a new value that is based on the value to be updated, which isn't possible by using loc alone.

Values of specific columns

To update values of specific columns based on their value:

df["A"] = df["A"].apply(lambda val: 2*val if val > 3 else val)
df
A B
0 3 5
1 8 6

Here, note the following:

  • we're doubling values in column A that are greater than 3.

  • since Series does not have applymap(~), we used apply(~) instead.

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?
thumb_down
Ask a question or leave a feedback...