Python - Replace values of a DataFrame with the value of another DataFrame in Pandas

To replace values of a DataFrame with the value of another DataFrame, use the replace() method in Pandas. This method allows you to substitute specific values across your DataFrame with new values from another source.

Creating Sample DataFrames

First, let's create two DataFrames to demonstrate the replacement process ?

import pandas as pd

# Create first DataFrame
dataFrame1 = pd.DataFrame({
    "Car": ["Audi", "Lamborghini"], 
    "Place": ["US", "UK"], 
    "Units": [200, 500]
})

print("DataFrame 1:")
print(dataFrame1)
DataFrame 1:
          Car Place  Units
0        Audi    US    200
1  Lamborghini    UK    500
import pandas as pd

# Create second DataFrame
dataFrame2 = pd.DataFrame({
    "Car": ["BMW", "Lexus"], 
    "Place": ["India", "Australia"], 
    "Units": [800, 1000]
})

print("DataFrame 2:")
print(dataFrame2)
DataFrame 2:
    Car       Place  Units
0   BMW       India    800
1  Lexus   Australia   1000

Using replace() Method

Now let's replace a value from DataFrame2 with a value from DataFrame1. We'll replace "Lexus" with "Audi" ?

import pandas as pd

dataFrame1 = pd.DataFrame({
    "Car": ["Audi", "Lamborghini"], 
    "Place": ["US", "UK"], 
    "Units": [200, 500]
})

dataFrame2 = pd.DataFrame({
    "Car": ["BMW", "Lexus"], 
    "Place": ["India", "Australia"], 
    "Units": [800, 1000]
})

print("Original DataFrame 2:")
print(dataFrame2)

# Get value from DataFrame2 to replace
old_value = dataFrame2['Car'][1]  # "Lexus"

# Get replacement value from DataFrame1  
new_value = dataFrame1['Car'][0]  # "Audi"

# Replace the value
dataFrame2 = dataFrame2.replace(old_value, new_value)

print("\nUpdated DataFrame 2:")
print(dataFrame2)
Original DataFrame 2:
    Car       Place  Units
0   BMW       India    800
1  Lexus   Australia   1000

Updated DataFrame 2:
   Car       Place  Units
0  BMW       India    800
1  Audi  Australia   1000

Multiple Value Replacement

You can also replace multiple values at once using a dictionary ?

import pandas as pd

dataFrame1 = pd.DataFrame({
    "Car": ["Audi", "Lamborghini"], 
    "Place": ["US", "UK"], 
    "Units": [200, 500]
})

dataFrame2 = pd.DataFrame({
    "Car": ["BMW", "Lexus"], 
    "Place": ["India", "Australia"], 
    "Units": [800, 1000]
})

print("Original DataFrame 2:")
print(dataFrame2)

# Replace multiple values using dictionary
replacement_dict = {
    "BMW": dataFrame1['Car'][0],      # Replace BMW with Audi
    "Lexus": dataFrame1['Car'][1]     # Replace Lexus with Lamborghini
}

dataFrame2_updated = dataFrame2.replace(replacement_dict)

print("\nUpdated DataFrame 2:")
print(dataFrame2_updated)
Original DataFrame 2:
    Car       Place  Units
0   BMW       India    800
1  Lexus   Australia   1000

Updated DataFrame 2:
          Car       Place  Units
0        Audi       India    800
1  Lamborghini   Australia   1000

Conclusion

The replace() method in Pandas provides a flexible way to substitute values between DataFrames. You can replace single values or multiple values using dictionaries for batch replacements.

Updated on: 2026-03-26T01:30:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements