How could I change all columns that have "change_" in the column name? For these columns I want to then conditionally replace rows. Want in general code for a larger dataset
df change_1 comment change_2
onee number two
three larger onee
[df[col].mask(df[col]=="onee","one") for col in df.columns if 'change_' in col]
Expected Output:
df change_1 comment change_2
one number two
three larger one
Advertisement
Answer
Use df.filter to filter out columns and then use df.replace:
In [555]: cols = df.filter(like='change_').columns
In [556]: df[cols] = df[cols].replace('onee', 'one')
In [557]: df
Out[557]:
change_1 comment change_2
0 one number two
1 three larger one