I want to replace the value in df if this value contains a partial string. My solution:
stimuli_dict = {'121': 'mp4', '212': 'mp3'}
stimuli_dict = {r"^{}".format(k): v for k, v in stimuli_dict.items()}
df['stimulus'] = df['stimulus'].replace(stimuli_dict, regex=True)
But it replaces only the partial string in a column. I want to replace the entire string with a dictionary value
CodePudding user response:
if you can add .* into your keys it would solve your problem. It is about regular expression. You need to tell that we are looking for something starts with 121 and rest is not important.
stimuli_dict = {'121.*': 'mp4', '212.*': 'mp3'}
"." means any character, "*" means previous character between zero and unlimited times. for further explanations, you can check regular expressions.