在同一索引

时间:2017-08-24 10:17:48

标签: python pandas dataframe

是否可以在同一索引上跨3个数据帧A,B和C执行功能?结束数据帧将具有与DF A相同的尺寸。例如:

def func(dfA.A0, dfB.A0, dfC.A0):
    if (dfA.A0 > dfB.A0):
        return dateDifferenceBetween(dfA.A0, dfC.A0)
    return 0

    Dataframe A         
    ABC DEF GHI JKL
01/01/2017  12/11/2017  03/01/2017  26/03/2017  10/07/2017
01/02/2017  21/08/2017  28/02/2017  08/09/2017  07/03/2017
01/03/2017  15/09/2017  07/01/2017  24/07/2017  15/11/2017
01/04/2017  01/01/2017  29/10/2017  13/06/2017  10/05/2017

    Dataframe B         
    ABC DEF GHI JKL
01/01/2017  22/11/2017  12/01/2017  08/04/2017  22/07/2017
01/02/2017  31/08/2017  11/03/2017  20/09/2017  14/03/2017
01/03/2017  26/09/2017  13/01/2017  05/08/2017  19/11/2017
01/04/2017  09/01/2017  02/11/2017  25/06/2017  21/05/2017


    Dataframe C         
    0           
01/01/2017  22/11/2017          
01/02/2017  31/08/2017          
01/03/2017  26/09/2017          
01/04/2017  09/01/2017          

enter image description here

1 个答案:

答案 0 :(得分:1)

IIUC,您可以遍历AB的列并将其传递给函数:

for i range(len(A.columns)):
    func(A.iloc[:, i], B.iloc[:, i], C.iloc[:, 0])

请注意,即使ABC都具有不同的列名,此方法仍然有效。

此外,func现在将接受3 pd.Series作为参数:

def func(sA, sB, sC):
    ...