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?
strip() function is used to remove or strip the leading and trailing space of the column in pandas dataframe.
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, .
To strip whitespaces from column names, you can use str. strip, str. lstrip and str. rstrip.
You can use DataFrame. select_dtypes to select string columns and then apply function str. strip .
Alternatively you could use str.strip
method:
rawlossDF['damage_description'] = rawlossDF['damage_description'].str.strip()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With