Pandas Dataframe Replace With Regex Doesn't Work
I have dataframe with many rows. I want to use pd.replace to replace values in entire columns. import pandas as pd import re list = ['MD 15241', 'MD', 'TD', 'TD 15487'] a = pd.Dat
Solution 1:
You need regex=True
in the replace
method:
b.replace(r'[A-Z]{2}', 'USA', inplace=True, regex=True)
b
#0
#0 USA 15241
#1 USA
#2 USA
#3 USA 15487
Solution 2:
def replace_string_inside_df(df):
df.replace(r'[^a-zA-Z0-9]', ' ', inplace=True, regex=True)
return df
print(replace_string_inside_df(df))
Post a Comment for "Pandas Dataframe Replace With Regex Doesn't Work"