I have a dataframe made by Pandas that I want to remove the empty space at the end of each column name. I tried something like:
raw_data.columns.values = re.sub(' $','',raw_data.columns.values)
But this is not working, anything I did wrong here?
I should have used rename with re.sub:
import re
raw_data = raw_data.rename(columns=lambda x: re.sub(' $','',x))
Or to change in-place:
raw_data.rename(columns=lambda x: re.sub(' $','',x), inplace=True)
raw_data.columns.values = re.sub(r'[ ]*$','',raw_data.columns.values)The answer from @Christian is probably right for this specific question, but to the more general question about replacing names in the columns, I would suggest to create a dictionary comprehension and pass it to the rename function:
df.rename(columns={element: re.sub(r'$ (.+)',r'\1', element, flags = re.MULTILINE) for element in df.columns.tolist()})
In my case, I wanted to add something to the beginning of each column, so:
df.rename(columns={element: re.sub(r'(.+)',r'x_\1', element) for element in df.columns.tolist()})
You can use the inplace=True parameter to actually make the change in the dataframe.
re.sub(' $', '')?type error