You can give functions to the rename
method. The str.strip()
method should do what you want:
In [5]: df
Out[5]:
Year Month Value
0 1 2 3
[1 rows x 3 columns]
In [6]: df.rename(columns=lambda x: x.strip())
Out[6]:
Year Month Value
0 1 2 3
[1 rows x 3 columns]
Note: that this returns a DataFrame
object and it’s shown as output on screen, but the changes are not actually set on your columns. To make the changes, either use this in a method chain or re-assign the df
variabe:
df = df.rename(columns=lambda x: x.strip())