pandas replace is not replacing value even with inplace=true

  • Last Update :
  • Techknowledgy :

You need to assign back

df = df.replace('white', np.nan)

or pass param inplace=True:

In[50]:
   d = {
      'color': pd.Series(['white', 'blue', 'orange']),
      'second_color': pd.Series(['white', 'black', 'blue']),
      'value': pd.Series([1., 2., 3.])
   }
df = pd.DataFrame(d)
df.replace('white', np.nan, inplace = True)
df

Out[50]:
   color second_color value
0 NaN NaN 1.0
1 blue black 2.0
2 orange blue 3.0

Neither one with inplace=True nor the other with regex=True don't work in my case. So I found a solution with using Series.str.replace instead. It can be useful if you need to replace a substring.

In[4]: df['color'] = df.color.str.replace('e', 'E!')
In[5]: df
Out[5]:
   color second_color value
0 whitE!white 1.0
1 bluE!black 2.0
2 orangE!blue 3.0

or even with a slicing.

In[10]: df.loc[df.color == 'blue', 'color'] = df.color.str.replace('e', 'E!')
In[11]: df
Out[11]:
   color second_color value
0 white white 1.0
1 bluE!black 2.0
2 orange blue 3.0

You might need to check the data type of the column before using replace function directly. It could be the case that you are using replace function on Object data type, in this case, you need to apply replace function after converting it into a string.

Wrong:

   df["column-name"] = df["column-name"].replace('abc', 'def')

Correct:

   df["column-name"] = df["column-name"].str.replace('abc', 'def')

When you use df.replace() it creates a new temporary object, but doesn't modify yours. You can use one of the two following lines to modify df:

df = df.replace('white', np.nan)
df.replace('white', np.nan, inplace = True)

{old_value:new_value}

df.replace({
   10: 100
}, inplace = True)

Python 3.10, pandas 1.4.2, inplace=True did not work for below example (column dtype int32), but reassigning it did.

df["col"].replace[[0, 130], [12555555, 12555555], inplace = True) # NOT work
df["col"] = df["col"].replace[[0, 130], [12555555, 12555555]) # worked

... and in another situation involving nans in text columns, the column needed typing in a pre-step (not just .str, as above):

df["col"].replace[["man", "woman", np.nan], [1, 2, -1], inplace = True) # NOT work
df["col"] = df["col"].str.replace[["man", "woman", np.nan], [1, 2, -1]) # NOT work

df["col"] = df["col"].astype(str) # needed
df["col"] = df["col"].replace[["man", "woman", np.nan], [1, 2, -1]) # worked

Suggestion : 2

pandas replace is not replacing value even with inplace=True,Pandas replace not working even with inplace=True,Replacing more than one substring value with pandas str.replace,How can I replace any value with an NAN that is not within a certain range of the previous value in a pandas series?

Assign back replaced values for avoid chained assignments:

m = cust_data_df['no_of_children'].notna()
d = {
   'Missing': 'Married'
}
cust_data_df.loc[m, 'marital_status'] = cust_data_df.loc[m, 'marital_status'].replace(d)

If need set all values:

cust_data_df.loc[m, 'marital_status'] = 'Married'

Suggestion : 3

The pandas dataframe replace() function allows you the flexibility to replace values in specific columns without affecting values in other columns.,The pandas dataframe replace() function is used to replace values in a pandas dataframe. It allows you the flexibility to replace a single value, multiple values, or even use regular expressions for regex substitutions. The following is its syntax:,In the above example, we only pass a single dictionary to the replace() function. The function infers the dictionary keys as values to replace in the dataframe and the corresponding keys as the values to replace them with. ,The replace() function replaces all occurrences of the value with the desired value. See the example below:

The pandas dataframe replace() function is used to replace values in a pandas dataframe. It allows you the flexibility to replace a single value, multiple values, or even use regular expressions for regex substitutions. The following is its syntax:

df_rep = df.replace(to_replace, value)

The replace() function replaces all occurrences of the value with the desired value. See the example below:

import pandas as pd

# sample dataframe
df = pd.DataFrame({
   'A': ['a', 'b', 'c'],
   'B': ['b', 'c', 'd']
})
print("Original DataFrame:\n", df)

# replace b with e
df_rep = df.replace('b', 'e')
print("\nAfter replacing:\n", df_rep)

Output:

Original DataFrame:
   A B
0 a b
1 b c
2 c d

After replacing:
   A B
0 a e
1 e c
2 c d

You can also have multiple replacements together. For example, if you want to replace a with b, b with c and c with d in the above dataframe, you can pass just a single dictionary to the replace function.

import pandas as pd

# sample dataframe
df = pd.DataFrame({
   'A': ['a', 'b', 'c'],
   'B': ['b', 'c', 'd']
})
print("Original DataFrame:\n", df)

# replace a with b, b with c, and c with d
df_rep = df.replace({
   'a': 'b',
   'b': 'c',
   'c': 'd'
})

print("\nAfter replacing:\n", df_rep)

To replace values within a dataframe via a regular expression match, pass regex=True to the replace function. Keep in mind that you pass the regular expression string to the to_replace parameter and the value to replace the matches to the value parameter. Also, note that regular expressions will only substitute for strings.

import pandas as pd

# sample dataframe
df = pd.DataFrame({
   'A': ['tap', 'cap', 'map'],
   'B': ['cap', 'map', 'tap']
})
print("Original DataFrame:\n", df)

# replace ap with op
df_rep = df.replace(to_replace = 'ap', value = 'op', regex = True)

print("\nAfter replacing:\n", df_rep)

Suggestion : 4

August 25, 2021March 8, 2022

To start things off, let’s begin by loading a Pandas dataframe. We’ll keep things simple so it’s easier to follow exactly what we’re replacing.

import pandas as pd

df = pd.DataFrame.from_dict({
   'Name': ['Jane', 'Melissa', 'John', 'Matt'],
   'Age': [23, 45, 35, 64],
   'Birth City': ['London', 'Paris', 'Toronto', 'Atlanta'],
   'Gender': ['F', 'F', 'M', 'M']
})

print(df)

This returns the following dataframe:

      Name Age Birth City Gender
      0 Jane 23 London F
      1 Melissa 45 Paris F
      2 John 35 Toronto M
      3 Matt 64 Atlanta M

The Pandas .replace() method takes a number of different parameters. Let’s take a look at them:

DataFrame.replace(
   to_replace = None,
   value = None,
   inplace = False,
   limit = None,
   regex = False,
   method = 'pad')

Of course, you could simply run the method twice, but there’s a much more efficient way to accomplish this. Here, we’ll look to replace London and Paris with Europe:

df['Birth City'] = df['Birth City'].replace(
   to_replace = ['London', 'Paris'],
   value = 'Europe')

print(df)

In the example below, we’ll replace London with England and Paris with France:

df['Birth City'] = df['Birth City'].replace(
   to_replace = ['London', 'Paris'],
   value = ['England', 'France'])

print(df)