繁体   English   中英

replace() 在 for 循环中不使用关键字 arguments

[英]replace() takes no keyword arguments, in for loop

我正在尝试将多列美元金额转换为浮点数,并编写了以下代码

    for column in wo.columns[14:21]:
        column = (column.replace( '[\$,)]','', regex=True )
                   .replace( '[(]','-',   regex=True ).replace('#NAME?','NaN', regex=True).astype(float))
    return column


And this is the error i get:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-276-f7c13cb3d0af> in <module>
      1 for column in wo.columns[14:19]:
----> 2     column = (column.replace( '[\$,)]','', regex=True )
      3                .replace( '[(]','-',   regex=True ).replace('#NAME?','NaN', regex=True).astype(float))
      4 return column
      5 

TypeError: replace() takes no keyword arguments

可能有什么问题? wo 是 dataframe 的名称,我用来加载 dataframe 的库是 Pandas,当我在其他单个列上使用代码时它返回一个错误,它工作正常,

它给出了一个错误,因为它不接受关键字 arguments; 在这种情况下regex=True 根据文档,它不存在。 此外, .replace仅适用于字符串,因此此处的数据类型应为str 另一方面,如果您想使用正则表达式,我建议您使用re.sub

柱子是什么类型? print(type(column))如果它是一个 str,它将使用 str.replace,它不需要 kwargs。 尝试删除regex=True

尝试删除 'regex = True' 将对我的案例有所帮助。

# df_num_key_split[name][var].replace(to_replace="[\[|\]|']|\s+|\.|\-", value='', regex=True)

在我这样做之后,它起作用了。

df_num_key_split[name][var].replace("[\[|\]|']|\s+|\.|\-", '')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM