删除pandas数据框的一列中的所有尾随空白 [英] Stripping all trailing empty spaces in a column of a pandas dataframe
问题描述
我有一个 pandas DF ,其中包含许多 string元素,其中包含这样的单词:
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'
我意识到由于前导空格,比较结果为 False .
I realized that the comparison was False due to the leading spaces.
尽管我可以通过遍历熊猫DF的每个元素来解决此问题,但是由于我拥有大量记录,因此该过程很慢.
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']
它返回:
'Frost '
这是怎么回事?
推荐答案
为此替换函数:
rawlossDF['damage_description'] = rawlossDF['damage_description'].map(lambda x: x.strip())
您几乎是对的,您需要摆脱strip()内的''
You almost had it right, you needed to get rid off the '' inside strip()
这篇关于删除pandas数据框的一列中的所有尾随空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!