Sum a list of Pandas DataFrames

前端 未结 1 1635
野性不改
野性不改 2020-12-16 04:30

Is there a way to sum multiple pandas DataFrames using syntax similar to pd.concat([df1, df2, df3, df4]). I understand from documentation that I can do df

相关标签:
1条回答
  • 2020-12-16 04:40

    Use reduce with add with parameter fill_value=0:

    np.random.seed(12)
    
    a = pd.DataFrame(np.random.randint(3, size=(5,3)), columns=list('abc'))
    b = pd.DataFrame(np.random.randint(3, size=(5,2)), columns=list('ab'))
    c = pd.DataFrame(np.random.randint(3, size=(5,2)), columns=list('ac'))
    print(a)
       a  b  c
    0  2  1  1
    1  2  0  0
    2  2  1  0
    3  1  1  1
    4  2  2  2
    
    print(b)
       a  b
    0  0  1
    1  0  0
    2  1  2
    3  1  2
    4  0  1
    
    print(c)
       a  c
    0  2  0
    1  2  2
    2  2  0
    3  0  2
    4  1  1
    

    from functools import reduce
    
    dfs = [a,b, c]
    d = reduce(lambda x, y: x.add(y, fill_value=0), dfs)
    print (d)
       a    b    c
    0  4  2.0  1.0
    1  4  0.0  2.0
    2  5  3.0  0.0
    3  2  3.0  3.0
    4  3  3.0  3.0
    
    0 讨论(0)
提交回复
热议问题