Why this error occuring "TypeError: Series.replace() takes from 1 to 3 positional arguments but 4 were given", Where I send 3 arguments?

huangapple go评论73阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年4月17日 05:37:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定