[英]replace() takes no keyword arguments, in for loop
I'm trying to convert multiple columns of dollar amount into float, and wrote the following code我正在尝试将多列美元金额转换为浮点数,并编写了以下代码
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
What might be wrong?可能有什么问题? wo is the name of the dataframe, the library i used to load the dataframe was Pandas, and when i used the code on other individual columns it worked fine, just when i used a for loop it return an error.
wo 是 dataframe 的名称,我用来加载 dataframe 的库是 Pandas,当我在其他单个列上使用代码时它返回一个错误,它工作正常,
It is giving an error because it doesn't accept keyword arguments;它给出了一个错误,因为它不接受关键字 arguments; in this case
regex=True
.在这种情况下
regex=True
。 As per the documentation , it doesn't exist.根据文档,它不存在。 Also,
.replace
is for strings only, so the type of data here should be str
.此外,
.replace
仅适用于字符串,因此此处的数据类型应为str
。 On the other hand, should you want to use regex, I'd recommend re.sub
另一方面,如果您想使用正则表达式,我建议您使用
re.sub
What type is column?柱子是什么类型?
print(type(column))
if its a str, it is going to use str.replace, which takes no kwargs. print(type(column))
如果它是一个 str,它将使用 str.replace,它不需要 kwargs。 Try deleting regex=True
.尝试删除
regex=True
。
Try to delete the 'regex = True' will be helpful from my case.尝试删除 'regex = True' 将对我的案例有所帮助。
# df_num_key_split[name][var].replace(to_replace="[\[|\]|']|\s+|\.|\-", value='', regex=True)
After I did this, it worked.在我这样做之后,它起作用了。
df_num_key_split[name][var].replace("[\[|\]|']|\s+|\.|\-", '')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.