Stripping all trailing empty spaces in a column of a pandas dataframe

headdetective picture headdetective · Dec 24, 2015 · Viewed 25.6k times · Source

I have a pandas DF that has many string elements that contains words like this:

'Frost                              '

Which has many leading white spaces in front of it. When I compare this string to:

'Frost'

I realized that the comparison was False due to the leading spaces.

Although I can solve this by iterating over every element of the pandas DF, the process is slow due to the large number of records I have.

This other approach should work, but it is not working:

rawlossDF['damage_description'] = rawlossDF['damage_description'].map(lambda x: x.strip(''))

So when I inspect an element:

rawlossDF.iloc[0]['damage_description']

It returns:

'Frost                              '

What's going on here?

Answer

Luis Miguel picture Luis Miguel · Dec 24, 2015

Replace your function with this:

rawlossDF['damage_description'] = rawlossDF['damage_description'].map(lambda x: x.strip())

You almost had it right, you needed to get rid off the '' inside strip()