i have pandas dataframe row contains data such as:
1 year 1 month 1 week 4 year 3 week
etc etc
i trying replace contains "month" or "week" 0
train_df.age["weeks" in train_df.age] = 0
and
for in train_df['age']: if "weeks" in i: = "0"
none of seem work.
any advice on how this? thanks.
use str.contains
:
train_df.loc[train_df['age'].str.contains(r'week|month'), 'age'] = 0
here pass regex pattern looks whether row contains either 'week' or 'month' , use boolean mask selectively update rows on interest:
in [4]: df.loc[df['age'].str.contains(r'week|month'), 'age'] = 0 df out[4]: age 1 year 1 0 1 0 4 year 3 0