[英]Type Error: Pandas Dataframe apply function, argument passing
By default, columns are all set to zero.默认情况下,列都设置为零。 Make entry as 1 at (row,column) where column name string present on URL column
在 URL 列上存在列名字符串的 (row,column) 处将条目设为 1
L # list that contains column names used to check if found on URL L # 包含用于检查是否在 URL 上找到的列名的列表
def generate(statement,col):
if statement.find(col) == -1:
return 0
else:
return 1
for col in L:
df3[col].apply(generate, args=(col))
I am a beginner, it throws and error:我是初学者,它抛出并出错:
/usr/local/lib/python3.6/dist-packages/pandas/core/series.py in f(x)
f(x) 中的 /usr/local/lib/python3.6/dist-packages/pandas/core/series.py
4195 4196 def f(x): -> 4197 return func(x, *args, **kwds) 4198 4199 else:4195 4196 def f(x): -> 4197 return func(x, *args, **kwds) 4198 4199 else:
TypeError: generate() takes 2 positional arguments but 9 were given
类型错误:generate() 需要 2 个位置参数,但给出了 9 个
Any suggestions would be helpful任何的意见都将会有帮助
Edit 1:编辑1:
after,后,
df3[col].apply(generate, args=(col,))
got error:得到错误:
> --------------------------------------------------------------------------- AttributeError Traceback (most recent call
> last) <ipython-input-162-508036a6e51f> in <module>()
> 1 for col in L:
> ----> 2 df3[col].apply(generate, args=(col,))
>
> 2 frames pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()
>
> <ipython-input-159-9380ffd36403> in generate(statement, col)
> 1 def generate(statement,col):
> ----> 2 if statement.find(col) == -1:
> 3 return 0
> 4 else:
> 5 return 1
>
> AttributeError: 'int' object has no attribute 'find'
Edit 2: "I missed to emphasize on URL column in for loop code will rectify that"编辑 2:“我没有在 for 循环代码中强调 URL 列将纠正这一点”
Edit 3: Updated and fixed to,编辑 3:更新并修复为,
def generate(statement,col):
if col in str(statement):
return 1
else:
return 0
for col in L:
df3[col] = df3['url'].apply(generate, col=col)
Thanks for all the support!感谢所有的支持!
创建 1 元素元组时,元素后需要一个逗号:args=(col,),否则括号将被忽略。
This seems to be a problem with passing parameter in args
.这似乎是在
args
传递参数的问题。 args
in apply
function will take the input as tuples and the same will be passed to the function. apply
函数中的args
将输入作为元组,并将其传递给函数。
Lets see one example to describe it,让我们看一个例子来描述它,
df = pd.DataFrame([['xyz', 'US'],['abc', 'MX'],['xyz', 'CA']], columns = ["Name", "Country"])
print(df)
Name Country
xyz US
abc MX
xyz CA
Create a function as required with extra arguments,根据需要创建一个带有额外参数的函数,
def generate(statement,col):
if statement.find(col) == -1:
return 0
else:
return 1
Consider L as the list, ['Name', 'Country']
将 L 视为列表,
['Name', 'Country']
Now, Lets apply the function generate
with extra arguments in loop现在,让我们在循环中应用带有额外参数的函数
generate
for col in L:
print(df[col].apply(generate, args=(col)))
TypeError: generate() takes 2 positional arguments but 5 were given
Now, we could see the error occurs because (col)
is a single element in tuple and so the args will take input as args=('N', 'A', 'M', 'E')
.现在,我们可以看到错误的发生,因为
(col)
是元组中的单个元素,因此 args 将输入作为args=('N', 'A', 'M', 'E')
。 Along with statement
now extra 4 inputs were given instead of just 1.现在除了
statement
外,还提供了 4 个额外的输入,而不仅仅是 1 个。
To avoid this situation, you can follow either of the below options为避免这种情况,您可以遵循以下任一选项
col
value to the parameter itself directlycol
值赋给参数本身df[col].apply(generate, col=col)
df[col].apply(generate, args=(col,))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.