Pandas Replace Multiple Values in Column based on Condition in Python [4 Methods]

Do you want to replace multiple values in a column based on the condition? In this Pandas tutorial, I will explain how Pandas replace multiple values in column based on condition in Python using different methods with examples.

To replace multiple values in a column based on specific conditions in pandas within Python, various methods like the replace() function, loc[] for conditional indexing, map() with a custom function, and DataFrame.mask() are employed.

Pandas Replace Multiple Values in Column based on Condition in Python

There are four different methods where we can see how Pandas replace multiple values in column based on condition in Python:

  1. replace() function
  2. loc[] function
  3. map() with a Function
  4. df.mask() function

Let’s see them one by one using some illustrative examples:

1. Pandas replace multiple values in a column based on the condition using replace() function

The df.replace() method in Pandas, is straightforward for replacing specific values in a DataFrame column. We can pass a dictionary specifying which values to replace and their new values.

Here is the code where we can use Pandas replace multiple values in a column based on the condition:

import pandas as pd

data = {'State': ['California', 'Texas', 'New York', 'Florida', 'Illinois'],
        'Population_Millions': [39.5, 29.0, 19.4, 21.5, 12.7]}
df = pd.DataFrame(data)
print("Before Replacing:\n", df)

df['Population_Millions'] = df['Population_Millions'].replace({39.5: 40, 19.4: 20})
print("After Replacing:\n", df)

Output:

Before Replacing:
         State  Population_Millions
0  California                 39.5
1       Texas                 29.0
2    New York                 19.4
3     Florida                 21.5
4    Illinois                 12.7
After Replacing:
         State  Population_Millions
0  California                 40.0
1       Texas                 29.0
2    New York                 20.0
3     Florida                 21.5
4    Illinois                 12.7

Below is a screenshot taken following the implementation of the code in the Pycharm editor.

Pandas Replace Multiple Values in Column based on Condition in Python

2. Replace Values in the Column based on Condition in Pandas using loc[] fucntion

The df.loc[] function in Pandas is ideal for condition-based indexing and allows us to modify values in a column based on a conditional statement. It’s especially useful for changing values under certain conditions.

Let’s see an example where Pandas replace multiple values in column based on condition in Python using the df.loc[] function:

import pandas as pd

data = {'State': ['California', 'Texas', 'New York', 'Florida', 'Illinois'],
        'GDP_Billions': [3000, 1800, 1700, 1000, 800]}
df = pd.DataFrame(data)
print("Before Replacing:\n", df)

df.loc[df['GDP_Billions'] < 1500, 'GDP_Billions'] *= 1.1
print("After Replacing:\n", df)

Output:

Before Replacing:
         State  GDP_Billions
0  California          3000
1       Texas          1800
2    New York          1700
3     Florida          1000
4    Illinois           800
After Replacing:
         State  GDP_Billions
0  California        3000.0
1       Texas        1800.0
2    New York        1700.0
3     Florida        1100.0
4    Illinois         880.0

Following the execution of the code in Pycharm, the resulting screenshot is displayed below.

pandas replace values in column based on multiple condition in Python

3. Pandas replace values in column based on multiple condition

When dealing with complex replacement logic, we can use the map() function in Python. It applies a function to each element in a column, enabling custom and varied transformations based on the function’s logic.

Here is the code to use the map() function for Pandas replace multiple values in column based on condition in Python:

import pandas as pd

data = {'City': ['Los Angeles', 'Houston', 'New York City', 'Miami', 'Chicago'],
        'Avg_Temperature_F': [75, 80, 60, 85, 50]}
df = pd.DataFrame(data)
print("Before Replacing:\n", df)
def adjust_temp(temp):
    return temp - 5 if temp > 70 else temp + 5

df['Avg_Temperature_F'] = df['Avg_Temperature_F'].map(adjust_temp)
print("After Replacing:\n", df)

Output:

Before Replacing:
             City  Avg_Temperature_F
0    Los Angeles                 75
1        Houston                 80
2  New York City                 60
3          Miami                 85
4        Chicago                 50
After Replacing:
             City  Avg_Temperature_F
0    Los Angeles                 70
1        Houston                 75
2  New York City                 65
3          Miami                 80
4        Chicago                 55

Following the execution of the code within the Pycharm editor, the subsequent screenshot displays the output.

pandas replace values in multiple columns based on condition in Python

4. Replace multiple columns Pandas

The mask() function in Python replaces values where a specified condition is true. Here we can directly mask and replace values based on conditions.

For instance, This is how Pandas Replace Multiple Values in Column based on Condition in Python using df.mask() function:

import pandas as pd

data = {'State': ['California', 'Texas', 'New York', 'Florida'],
        'Sales_Tax_Percentage': [7.25, 6.25, 8.875, 6]}
df = pd.DataFrame(data)
print("Before Replacing:\n", df)

df['Sales_Tax_Percentage'] = df['Sales_Tax_Percentage'].mask(df['Sales_Tax_Percentage'] > 7, 7)
print("After Replacing:\n", df)

Output:

Before Replacing:
         State  Sales_Tax_Percentage
0  California                 7.250
1       Texas                 6.250
2    New York                 8.875
3     Florida                 6.000
After Replacing:
         State  Sales_Tax_Percentage
0  California                  7.00
1       Texas                  6.25
2    New York                  7.00
3     Florida                  6.00

The screenshot provided below illustrates the results post-execution of the code in the Pycharm editor.

Pandas replace values in multiple columns based on the condition in Python

Conclusion

Here, I have explained how Pandas Replace Multiple Values in Column based on Condition in Python, using four different methods: the replace() function for direct replacements, the loc[] function for condition-based changes, the map() function with a custom function for more complex transformations, and the DataFrame.mask() function for conditional masking.

Each method offers unique advantages, making them suitable for a variety of data manipulation tasks.

You may also like to read: