簡體   English   中英

將 dataframe 的行作為參數傳遞給 python 中的 function

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM