Removing all trailing white spaces in a pandas dataframe column

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

'Frost ' 

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

 'Frost' 

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

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

This other approach should work, but it does not work:

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

So when I check the element:

 rawlossDF.iloc[0]['damage_description'] 

It returns:

 'Frost ' 

What's going on here?

+5
source share
2 answers

Replace the function as follows:

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

You had almost everything right, you had to get rid of the inner strip ()

+15
source

Alternatively, you can use the str.strip method:

 rawlossDF['damage_description'] = rawlossDF['damage_description'].str.strip() 
+14
source

Source: https://habr.com/ru/post/1239106/


All Articles