英文:
Why this error occuring "TypeError: Series.replace() takes from 1 to 3 positional arguments but 4 were given", Where I send 3 arguments?
问题
I want to replace a "\\N" value with "O" by pandas.
> But it's showing me this error:
df["amount_recived"].replace("\N", "0", True)
TypeError: Series.replace() takes from 1 to 3 positional arguments but 4 were given
> My script was:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df1 = pd.read_csv("C:/Users/sanyk/Downloads/SM_Data/Data_Set.csv")
df2 = pd.read_csv("C:/Users/sanyk/Downloads/SM_Data/Data_Set_2nd_part.csv")
df = pd.concat([df1, df2])
Less Important column value manipulating
df["amount_recived"].replace("\N", "0", True)
英文:
I want to replace a "\\N" value with "O" by pandas.
> But it's showing me this error:
df["amount_recived"].replace("\\N", "0", True)
TypeError: Series.replace() takes from 1 to 3 positional arguments but 4 were given
> My script was:
import pandas as pd
import numpy as np
# import matplotlib.pyplot as plt
df1 = pd.read_csv("C:/Users/sanyk/Downloads/SM_Data/Data_Set.csv")
df2 = pd.read_csv("C:/Users/sanyk/Downloads/SM_Data/Data_Set_2nd_part.csv")
df = pd.concat([df1, df2])
# Less Important column value manipulating
df["amount_recived"].replace("\\N", "0", True)
As you can see that there I just sent in "replace()" method "\\N" as the argument value of to_replace, "0" as "value" and regex=True. That means I didn't send anything wrong to the "replace()" method. Official Doc
答案1
得分: 2
Remove the True argument in your replace:
将replace函数中的True参数移除:
df["amount_received"] = df["amount_received"].replace("\N", "0")
True is your 3rd unnecessary arg:
True是您的第三个不必要的参数。
英文:
Correct this by removing the True arg in your replace :
df["amount_recived"] = df["amount_recived"].replace("\\N", "0")
True is your 3rd unnecessary arg
答案2
得分: 2
以下是您要翻译的内容:
从文档中关于函数定义的部分:
> “*”或“*identifier”后面的参数是关键字参数,只能通过关键字参数传递。
如果您查看文档中replace的定义:
Series.replace(to_replace=None, value=_NoDefault.no_default, *, inplace=False, limit=None, regex=False, method=_NoDefault.no_default)
您可以看到regex参数在*后面列出,这意味着它必须作为关键字参数使用。所以您的代码的正确形式是:
df["amount_recived"] = df["amount_recived"].replace("\\N", "0", regex=True)
英文:
From the documentation on function definitions:
> Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed by keyword arguments.
If you look at the definition of replace in the docs:
Series.replace(to_replace=None, value=_NoDefault.no_default, *, inplace=False, limit=None, regex=False, method=_NoDefault.no_default)
You can see that the regex parameter is listed after a *, which means it must be used as a keyword parameter. So the correct form of your code is:
df["amount_recived"] = df["amount_recived"].replace("\\N", "0", regex=True)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论