gpt4 book ai didi

python - 剥离 Pandas 数据框列中的所有尾随空格

转载 作者:太空狗 更新时间:2023-10-29 18:13:36 25 4
gpt4 key购买 nike

我有一个 pandas DF,它有很多 string 元素,其中包含这样的词:

'Frost                              '

它前面有许多前导空白。当我将这个字符串与:

'Frost'

我意识到由于前导空格,比较是 False

虽然我可以通过遍历 pandas DF 的每个元素来解决这个问题,但由于我有大量记录,这个过程很慢。

这种其他方法应该有效,但它不起作用:

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

所以当我检查一个元素时:

rawlossDF.iloc[0]['damage_description']

它返回:

'Frost                              '

这是怎么回事?

最佳答案

或者你可以使用 str.strip方法:

rawlossDF['damage_description'] = rawlossDF['damage_description'].str.strip()

关于python - 剥离 Pandas 数据框列中的所有尾随空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34446767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com