简体   繁体   English

将 dataframe 的行作为参数传递给 python 中的 function

[英]passing rows of dataframe as argument to a function in python

I am trying to pass columns of a dataframe as arguments to function in row wise manner but I am ending in an error.我正在尝试将 dataframe 的列作为 arguments 以行方式传递给 function,但我以错误结束。 I tried to do by loop as my actual dataframe and function requires loop only.我尝试循环执行,因为我的实际 dataframe 和 function 只需要循环。

def sum(x, y, z):
     return x + y + z
Input = pd.DataFrame({'A': [1, 2,3], 'B': [10, 20,22],'C':[4,5,6]})
output = pd.DataFrame({'output': [15, 27,31]})

##What I tried, but this tells me it needs more arguments
for a in Input:
    sum(a)

Could anyone help me, as I need to iterate all the rows of dataframe as in argument to a function so in short number of columns of dataframe is equal to number of arguments in functions Could anyone help me, as I need to iterate all the rows of dataframe as in argument to a function so in short number of columns of dataframe is equal to number of arguments in functions

Try this尝试这个

Input.apply(lambda row : sum(row.A, row.B, row.C), axis=1)

axis = 1, mean on column level.轴 = 1,列级别的平均值。 can refer at pandas apply It asks for more arguments because it does not know which column you want to do operation.可以参考pandas apply它要求更多的 arguments 因为它不知道您要操作哪一列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM