YanRemes
YanRemes

Reputation: 337

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

Upvotes: 0

Views: 1973

Answers (2)

Ayush Goel
Ayush Goel

Reputation: 375

One way would be to first merge the dataframes, and then carry out the sum.

df1 = df1.merge(df2, left_on='dt', right_on='dt', how='outer')

The column names may end with an '_x' or '_y' to allow you to differentiate. You can then do a normal dataframe sum.

Upvotes: 2

Deepak Tatyaji Ahire
Deepak Tatyaji Ahire

Reputation: 5309

Try this:

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

print(df)

PS: This is ensure all datetimes to exist.

Upvotes: 3

Related Questions