Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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, like so:

df.columns

The result looks like this:

Index(['Year', 'Month ', 'Value'])

Consequently, I can't run

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?

1 Answer

0 votes
by (41.4k points)

1.Firstly, pass the function into rename method

2.Then, use str.strip() method to strip the whitespace.

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]

Browse Categories

...