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
check_circle
Mark as learned
thumb_up
5
thumb_down
0
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

Conditionally updating values of a DataFrame in Pandas

schedule Aug 11, 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!

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.

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...