zach
zach

Reputation: 31073

apply changes to the column names of a dataframe

I have a dataframe and I would like to change the column names. Currently I am using the method below which involves transposing, reindexing, and transposing back. Theres got to be a simpler way.....

any suggestions are appreciated

import pandas as pd

#make a dataframe with wacky column names
d = {'garbled #### one' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd']),
     'garbled ### two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)

#fix the column names by transposing, reseting index, string manipulation,
#and transposing back  
df = df.T
df = df.reset_index()
df['index'] = df['index'].apply(lambda x: x.split()[0]+ " " +x.split()[2])
df = df.set_index('index')
df = df.T
df

index   garbled two garbled one
a    1   1
b    2   2
c    3   3
d    4   4

thanks, zach cp

Upvotes: 0

Views: 1497

Answers (2)

Wouter Overmeire
Wouter Overmeire

Reputation: 69276

rename_axis alows to rename without creating/removing columns. Renaming can be done with a function or a one to one mapping (dict-like), a mapping can be partial (it is not necessary to include all names).

In [42]: df
Out[42]: 
   garbled #### one  garbled #### two
a                 1                 1
b                 2                 2
c                 3                 3
d                 4                 4

In [43]: df.rename_axis(lambda x: x.split()[0]+ " " +x.split()[2])
Out[43]: 
   garbled one  garbled two
a            1            1
b            2            2
c            3            3
d            4            4

In [44]: df.rename_axis({'garbled #### one': 'one', 'garbled #### two': 'two'})
Out[44]: 
   one  two
a    1    1
b    2    2
c    3    3
d    4    4

Upvotes: 2

Eike
Eike

Reputation: 2223

Maybe I'm underestimating the problem, but here is a rather trivial method.

Get the list of column names (really a pd.Index) with:

df.columns

Iterate over the column names to see if any is garbled. If you find a column with a garbled name, create a new column with a good name, and delete the old column, like this:

df["good-one"] = df["garbled #### one"]
del df["garbled #### one"]

Unless the table is huge, and the amount of copied data is a concern, this will work.

Upvotes: 1

Related Questions