【发布时间】:2020-02-04 01:20:49
【问题描述】:
我正在尝试将多列美元金额转换为浮点数,并编写了以下代码
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 是数据框的名称,我用来加载数据框的库是 Pandas,当我在其他单独的列上使用代码时它工作正常,只是当我使用 for 循环时它返回错误。
【问题讨论】:
-
如果您使用外部库,请将其添加到问题的标签中。并显示“wo”的定义。
-
如果您使用 pandas,则需要将替换应用到 DataFrame 或 Series 的
str,如下所示:column.str.replace。每次都必须在replace函数之前添加str部分。 -
另外,pandas regex=True 是默认值,不需要指定。
标签: python for-loop replace typeerror keyword-argument