I have a DataFrame with a variable I want to map, using a dictionary where the keys are not "normal" strings, but regular expressions.
import pandas as pd
import re
df = pd.DataFrame({'cat': ['A1', 'A2', 'B1']})
What I would like to do is df['cat'].map({'A\d': 'a', 'B1': 'b'}), but A\d seems not be interpreted as a regex. In this simple MWE I could do df['cat'].map({'A1': 'a', 'A2': 'a', 'B1': 'b'}), but in the real world, the regex is much more complicated. Also the dictionary is much more complicated, so that the solution here (which requires to add start and end statementents and apply re.compile around the keys) is not feasable.