How can I strip the whitespace from Pandas DataFrame headers?

Spike Williams picture Spike Williams · Feb 6, 2014 · Viewed 65.2k times · Source

I am parsing data from an Excel file that has extra white space in some of the column headings.

When I check the columns of the resulting dataframe, with df.columns, I see:

Index(['Year', 'Month ', 'Value'])
                     ^
#                    Note the unwanted trailing space on 'Month '

Consequently, I can't do:

df["Month"]

Because it will tell me the column is not found, as I asked for "Month", not "Month ".

My question, then, is how can I strip out the unwanted white space from the column headings?

Answer

TomAugspurger picture TomAugspurger · Feb 6, 2014

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 take place, use:

  1. Use the inplace=True argument [docs]
df.rename(columns=lambda x: x.strip(), inplace=True)
  1. Assign it back to your df variable:
df = df.rename(columns=lambda x: x.strip())