[英]passing rows of dataframe as argument to a function in python
我正在尝试将 dataframe 的列作为 arguments 以行方式传递给 function,但我以错误结束。 我尝试循环执行,因为我的实际 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
尝试这个
Input.apply(lambda row : sum(row.A, row.B, row.C), axis=1)
轴 = 1,列级别的平均值。 可以参考pandas apply它要求更多的 arguments 因为它不知道您要操作哪一列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.