Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 348
headdetective Avatar asked Dec 24 '15 03:12

headdetective


People also ask

How do you delete trailing spaces in pandas DataFrame column?

strip() function is used to remove or strip the leading and trailing space of the column in pandas dataframe.

How do you strip space in pandas?

lstrip() is used to remove spaces from the left side of string, str. rstrip() to remove spaces from right side of the string and str. strip() removes spaces from both sides. Since these are pandas function with same name as Python's default functions, .

How do I strip spaces in a column name?

To strip whitespaces from column names, you can use str. strip, str. lstrip and str. rstrip.

How do I strip a column in a data frame?

You can use DataFrame. select_dtypes to select string columns and then apply function str. strip .


1 Answers

Alternatively you could use str.strip method:

rawlossDF['damage_description'] = rawlossDF['damage_description'].str.strip()
like image 179
Anton Protopopov Avatar answered Oct 04 '22 14:10

Anton Protopopov