Denver Dang
Denver Dang

Reputation: 2615

Sum columns from multiple data frames by date

In my head this sounds like something that should be done natively, but I just haven't been able to find the function in Pandas that do this (yes, I might just be an idiot). Basically, let's assume I have:

df1 =  
      x     y     date
      --------------------
      1     2     1/1/2020
      2     3     2/1/2020
      3     4     3/1/2020
      4     5     4/1/2020 

df2 =  
      x     y     date
      --------------------
      3     4     1/1/2020
      4     5     2/1/2020
      5     6     3/1/2020
      6     7     4/1/2020 

df3 =  
      x     y     date
      --------------------
      5     6     1/1/2020
      6     7     2/1/2020
      7     8     3/1/2020
      8     9     4/1/2020 

The resulting df should then be:

df_result =  
      x     y     date
      --------------------
      9     12    1/1/2020
      12    15    2/1/2020
      15    18    3/1/2020
      18    21    4/1/2020 

Upvotes: 0

Views: 531

Answers (2)

Chandrabhan
Chandrabhan

Reputation: 24

You can join the data frame by date column and simply add the X and Y columns

Upvotes: 0

jezrael
jezrael

Reputation: 862451

Use concat with aggregate sum:

df = pd.concat([df1, df2, df3]).groupby('date', as_index=False).sum()

Upvotes: 3

Related Questions