Pandas replace()

The replace() method in Pandas is used to replace values in a DataFrame.

Example

import pandas as pd

# create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3, 4],
    'B': [5, 6, 7, 8]
})

# replace the value 2 with 200 df_replaced = df.replace(2, 200)
print(df_replaced) ''' Output A B 0 1 5 1 200 6 2 3 7 3 4 8 '''

replace() Syntax

The syntax of the replace() method in Pandas is:

df.replace(to_replace, value, inplace=False, regex=False)

replace() Arguments

The replace() method takes following arguments:

  • to_replace - specifies what we want to replace
  • value - specifies the replacement value
  • inplace (optional) - modifies the DataFrame in place if True
  • regex (optional) - enables regular expression matching if True

replace() Return Value

The replace() method in Pandas returns a new DataFrame with the specified replacements applied.


Example 1: Replace Value(s) in a DataFrame

import pandas as pd

# create a sample DataFrame
data = {'A': [1, 2, 3, 4],
        'B': [5, 6, 7, 8]}
df = pd.DataFrame(data)

# replace a single value 2 with 200 df_single_replace = df.replace(2, 200)
# replace multiple values 1 with 100 and 3 with 300 df_multiple_replace = df.replace({1: 100, 3: 300})
print("Original DataFrame:") print(df) print("\nDataFrame after single replace:") print(df_single_replace) print("\nDataFrame after multiple replace:") print(df_multiple_replace)

Output

Original DataFrame:
   A  B
0  1  5
1  2  6
2  3  7
3  4  8

DataFrame after single replace:
     A  B
0    1  5
1  200  6
2    3  7
3    4  8
DataFrame after multiple replace:
    A   B
0  100  5
1    2  6
2  300  7
3    4  8

In the above example, we used replace(2, 200) to replace a single value 2 with 200 in df_single_replace.

We also used replace({1: 100, 3: 300}) to replace multiple values 1 with 100 and 3 with 300 in df_multiple_replace.


Example 2: Replace Values in a Specific Column

import pandas as pd

# create a DataFrame
data = {'A': [1, 2, 3, 4],
        'B': [5, 6, 7, 8]}
df = pd.DataFrame(data)

# replace values in column 'A' df['A'].replace(2, 200, inplace=True)
print(df)

Output

     A    B
0    1    5
1  200    6
2    3    7
3    4    8

Here, we used df['A'] to access and modify values in column A.

We replaced the value 2 with 200 in column A using the replace() method with inplace=True.

When inplace=True is used, it means that the df DataFrame is modified directly, meaning, it doesn't return a new DataFrame but instead alters df itself.


Example 3: Use Regular Expressions to Replace Values

import pandas as pd

# create a DataFrame
data = {'Text': ['apple_1', 'banana_2', 'cherry_3']}
df = pd.DataFrame(data)

# use regular expression to replace values in the 'Text' column df['Text'] = df['Text'].replace(r'(\d+)', 'X', regex=True)
print(df)

Output

     Text
0   apple_X
1  banana_X
2  cherry_X

In this example, we have the df DataFrame with the Text column containing strings.

SInce we want to replace all digits in the Text column with the letter X, we use replace() with regex=True and the regular expression r'(\d+)' to match one or more digits.

All the digits in the Text column are replaced with the replacement value X.

Note: To learn more about Regular Expressions, please visit Python RegEx.


Example 4: Use a Dictionary to Replace

import pandas as pd

# create a DataFrame
data = {'A': [1, 2, 3, 4],
        'B': [5, 6, 7, 8]}
df = pd.DataFrame(data)

# define a dictionary for replacement
replacement_dict = {2: 200, 4: 400}

# replace values using the dictionary df.replace(replacement_dict, inplace=True)
print(df)

Output

    A   B
0   1   5 
1  200  6
2  3    7 
3  400  8

In the above example, we defined a dictionary replacement_dict where keys represent values to be replaced and values represent their replacements.

Then we used replace() to replace the values in the df DataFrame based on the replacement_dict.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges