search
Search
Unlock 100+ guides
search toc
close
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
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# 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 B0 3 51 4 6 ```

# Conditionally updating all values

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

``` df[df > 3] = 10df A B0 3 101 10 10 ```

## Explanation

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

``` df > 3 A B0 False True1 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 B0 NaN 51 4.0 6 ```

We can then update the values using `=` like so:

``` df[df > 3] = 10df A B0 3 101 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 B0 3 51 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"] = 10df A B0 3 51 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 False1 TrueName: 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] = 10df A B0 3 51 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"] = 10df A B0 3 51 10 6 ```

# Conditionally updating values based on their value

Consider the following DataFrame:

``` df = pd.DataFrame({"A":[3,4],"B":[5,6]})df A B0 3 51 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 B0 3 101 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 B0 3 51 8 6 ```

Here, note the following:

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
5
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!