Search code examples
pythonpandasdataframesum

how to sum two dataframes python


I have two dataframes

tickers                        dt  AAPL  AMC  AMZN  ...  TH  TSLA  VIAC  WKHS
0       2021-03-22 00:00:00+00:00     0    0     0  ...   0     1     0     0
1       2021-03-23 00:00:00+00:00     0    0     0  ...   0     1     0     0
2       2021-03-24 00:00:00+00:00     1    2     0  ...   0     0     0     0
3       2021-03-25 00:00:00+00:00     0    0     0  ...   0     0     0     0
4       2021-03-26 00:00:00+00:00     0    2     0  ...   0     4     0     0
tickers                        dt  AAPL  AMC  AMZN  ...  TH  TSLA  VIAC  WKHS
0       2021-03-19 00:00:00+00:00     0    0     0  ...   0     0     0     0
1       2021-03-20 00:00:00+00:00     0    0     0  ...   0     0     0     0
2       2021-03-21 00:00:00+00:00     0    0     0  ...   0     0     0     0
3       2021-03-22 00:00:00+00:00     0    0     0  ...   0     3     0     0
4       2021-03-23 00:00:00+00:00     0    0     0  ...   0     3     0     0

I want to sum each row by corresponding row from another dataframe. You can see that some time from one dataframe doesn't exist in another dataframe. I also want to consider them and include in the new dataframe


Solution

  • Try this:

    df = pd.concat([df1, df2]).groupby(['dt']).sum().reset_index()
    
    print(df)
    

    PS: This is ensure all datetimes to exist.