Need to replace the all the values to ABC where ever ABC is a substring in dataframe.
df_updated = df.replace(to_replace='ABC.*', value='ABC', regex=True)
The above replaces all the "ABC1234"
, ABCrhdj"
, "ABC7544"
etc type to "ABC"
but need to also replace "1243ABC124"
, "wereABC2445"
, etc to "ABC"
.
I know it is easy but not able to get the exact code. Thanks.
CodePudding user response:
You could just add the same expression to match any number of characters (.*
) at the beginning of your regex (.*ABC.*
) to match values that do not necessarily start with "ABC"
:
df_updated = df.replace(to_replace=".*ABC.*", value="ABC", regex=True)
CodePudding user response:
In your regex, you are specifying anything that starts with ABC, So it is replacing any string starting with ABC only. Please add .* in the beginning like this:
df_updated = df.replace(to_replace ='.*ABC.*', value = 'ABC', regex = True)
This will match any string containing ABC and replace it with your expected value.