python pandas rename multiple column headers the same way python pandas rename multiple column headers the same way pandas pandas

python pandas rename multiple column headers the same way


pd.DataFrame.add_suffix

df.add_suffix('X')   HeaderAX  HeaderBX  HeaderCX0       476      4365       457

And the sister method
pd.DataFrame.add_prefix

df.add_prefix('X')   XHeaderA  XHeaderB  XHeaderC0       476      4365       457

You can also use the pd.DataFrame.rename method and pass a function. To accomplish the same thing:

df.rename(columns='{}X'.format)   HeaderAX  HeaderBX  HeaderCX0       476      4365       457

In this example, '{}X'.format is a function that takes a single argument and appends an 'X'

The advantage of this method is that you can use inplace=True if you chose to.


From SO post. Let's try using a lambda function in rename:

df.rename(columns = lambda x: x+'X', inplace = True)


df.columns = list(map(lambda s: s+'X', df.columns))