I am attempting to replace a string within a pandas dataframe, with a string pulled from a dictionary which contains multiple sets of parentheses. When running the script, I get an error for match groups, and the string is not replaced. I'm fairly confident that this error is caused by the parentheses.
To resolve, I have been attempting to use regular expression pattern matching using the str.contains() method. I have reviewed other solutions provided on stackoverflow, but haven't been successful in resolving my error.
Here is some script I am using for testing purposes. It's important that the parentheses are maintained in the strings (i.e. I don't to have to remove them):
import pandas as pd
import numpy as np
dict= {'2017() (pat)':'2000',
'2018() (pat)':'2001'}
df = pd.DataFrame({'YEAR': ['test2017end','test2018end','test2019end'],
'MONTH': ['Jan','Feb','Mar'],
'DD': ['1','12','22']})
for init, repl in dict.items():
df.loc[df['YEAR'].str.contains(init),'YEAR'] = repl
print(df)
Can someone please provide guidance on using pattern matching so that the strings are properly replaced?
Thanks!
dict