I'm trying to rename over a bunch of columns in my df in Python. Because there are over a 1000 that should be renamed I'm trying to do it with regex since I saw that Python allows you to do this. More specifically, every column ending in _Sum should be renamed, with the _Sum part, replaced by '_max' (ex.: column1_Sum -> column1_max). I've tried following code:
df = df.rename(columns=lambda x: re.sub('(.+)_Sum$','$1_max',x))
But it just replaces every columnname literally with '$1_max'. I've worked previously with regex in other programs and I always thought that $1 captures your previous group, in this case, everything before the '_', so I don't really know what I'm doing wrong here.
r"\1_max"instead