Replace Values in Pandas DataFrame

Here are 4 ways to replace values in Pandas DataFrame:

(1) Replace a single value with a new value:

df["column_name"] = df["column_name"].replace(["old_value"], "new_value")

(2) Replace multiple values with a new value:

df["column_name"] = df["column_name"].replace(["1st_old_value", "2nd_old_value", ...], "new_value")

(3) Replace multiple values with multiple new values:

df["column_name"] = df["column_name"].replace(["1st_old_value", "2nd_old_value", ...], ["1st_new_value", "2nd_new_value", ...])

(4) Replace a single value with a new value across the entire DataFrame:

df = df.replace(["old_value"], "new_value")

Examples

To start, create a DataFrame with the following data:

import pandas as pd

data = {
"first_set": ["Green", "Green", "Green", "Blue", "Blue", "Red", "Red", "Red"],
"second_set": ["Yellow", "Yellow", "Yellow", "White", "White", "Blue", "Blue", "Blue"],
}

df = pd.DataFrame(data)

print(df)

Run the code in Python, and you’ll get the following DataFrame:

  first_set  second_set
0     Green      Yellow
1     Green      Yellow
2     Green      Yellow
3      Blue       White
4      Blue       White
5       Red        Blue
6       Red        Blue
7       Red        Blue

Example 1: Replace a single value with a new value

To replace the “Blue” values with the “Green” values under the “first_set” column:

df["first_set"] = df["first_set"].replace(["Blue"], "Green")

The complete code:

import pandas as pd

data = {
"first_set": ["Green", "Green", "Green", "Blue", "Blue", "Red", "Red", "Red"],
"second_set": ["Yellow", "Yellow", "Yellow", "White", "White", "Blue", "Blue", "Blue"],
}

df = pd.DataFrame(data)

df["first_set"] = df["first_set"].replace(["Blue"], "Green")

print(df)

Notice that all the “Blue” values got replaced with the “Green” values under the first column:

  first_set  second_set
0     Green      Yellow
1     Green      Yellow
2     Green      Yellow
3     Green       White
4     Green       White
5       Red        Blue
6       Red        Blue
7       Red        Blue

Example 2: Replace multiple values with a new value

To replace the “Blue” and the “Red” colors with the “Green” color under the “first_set” column.

df["first_set"] = df["first_set"].replace(["Blue", "Red"], "Green")

The full code:

import pandas as pd

data = {
"first_set": ["Green", "Green", "Green", "Blue", "Blue", "Red", "Red", "Red"],
"second_set": ["Yellow", "Yellow", "Yellow", "White", "White", "Blue", "Blue", "Blue"],
}

df = pd.DataFrame(data)

df["first_set"] = df["first_set"].replace(["Blue", "Red"], "Green")

print(df)

Both the “Blue” and the “Red” colors got replaced with the “Green” color under the first column:

  first_set  second_set
0     Green      Yellow
1     Green      Yellow
2     Green      Yellow
3     Green       White
4     Green       White
5     Green        Blue
6     Green        Blue
7     Green        Blue

Example 3: Replace multiple values with multiple new values

To replace (under the “first_set” column):

  • The “Blue” color with the “Green” color; and
  • The “Red” color with the “White” color
df["first_set"] = df["first_set"].replace(["Blue", "Red"], ["Green", "White"])

The full syntax:

import pandas as pd

data = {
"first_set": ["Green", "Green", "Green", "Blue", "Blue", "Red", "Red", "Red"],
"second_set": ["Yellow", "Yellow", "Yellow", "White", "White", "Blue", "Blue", "Blue"],
}

df = pd.DataFrame(data)

df["first_set"] = df["first_set"].replace(["Blue", "Red"], ["Green", "White"])

print(df)

Notice that the “Blue” became “Green” and the “Red” became “White” under the first column:

  first_set  second_set
0     Green      Yellow
1     Green      Yellow
2     Green      Yellow
3     Green       White
4     Green       White
5     White        Blue
6     White        Blue
7     White        Blue

Example 4: Replace values across the entire DataFrame

To replace the “Blue” color with the “Green” color across the entire DataFrame:

df = df.replace(["Blue"], "Green")

The complete code:

import pandas as pd

data = {
"first_set": ["Green", "Green", "Green", "Blue", "Blue", "Red", "Red", "Red"],
"second_set": ["Yellow", "Yellow", "Yellow", "White", "White", "Blue", "Blue", "Blue"],
}

df = pd.DataFrame(data)

df = df.replace(["Blue"], "Green")

print(df)

Here, the “Blue” became “Green” across all the columns in the DataFrame:

  first_set  second_set
0     Green      Yellow
1     Green      Yellow
2     Green      Yellow
3     Green       White
4     Green       White
5       Red       Green
6       Red       Green
7       Red       Green

And if you decide to replace two colors, such as “Blue” and “Red” into “Green,” then apply this syntax:

import pandas as pd

data = {
"first_set": ["Green", "Green", "Green", "Blue", "Blue", "Red", "Red", "Red"],
"second_set": ["Yellow", "Yellow", "Yellow", "White", "White", "Blue", "Blue", "Blue"],
}

df = pd.DataFrame(data)

df = df.replace(["Blue", "Red"], "Green")

print(df)

Both the “Blue” and the “Red” colors would be replaced with the “Green” color across the entire DataFrame:

  first_set  second_set
0     Green      Yellow
1     Green      Yellow
2     Green      Yellow
3     Green       White
4     Green       White
5     Green       Green
6     Green       Green
7     Green       Green

Leave a Comment