Assume that I have a DataFrame with dates in European Languages:
In[0]: df_date
Out[0]:
date
0 ene 1 2021
1 1 ene 2021
2 mar. 1 2021
3 5 mars. 2021
4 10 abr 2021
5 abr. 1 2021
And I have another DataFrame with corresponding string mappings
In[0]: df_translate
Out[0]:
foreign eng
0 ene jan
1 feb feb
2 mar mar
3 abr apr
4 may may
5 mars mar
The way I go about replacing these is usually with converting the mapping DataFrame to a dictionary, and then use Series.str.replace()
date_replace_dict = df_translate.set_index('foreign',drop=True).to_dict()['eng']
for k,v in date_replace_dict.items():
df_date['date'] = df['date'].str.replace(k,v,regex=False)
I have found this process to be a bit slow, I was wondering if there is a better/faster more pythonic way of doing it.
I cannot use the Series.replace() method because apparently it requires an exact match on the string.
Also please note that, this is a more general question, in the sense that I happen to do this operation a lot on different types of columns containing different types of strings and substrings. So the substrings to be replaced are not necessarily at the start of the string or in any other algorithmically determinable place.
If this is a good way of going about it, then it is fine. But python tends to make me feel like I am doing something very wrong whenever I use a loop for achieveing something :)