Summing two DataFrames by index

I have the following

df1 = pd.DataFrame([1, 1, 1, 1, 1], index=[ 1, 2, 3, 4 ,5 ], columns=['A'])  
df2 = pd.DataFrame([ 1, 1, 1, 1, 1], index=[ 2, 3, 4, 5, 6], columns=['A']) 

I want to return a DataFrame that will be the sum of two for each row:

df = pd.DataFrame([ 1, 2, 2, 2, 2, 1], index=[1, 2, 3, 4, 5, 6], columns=['A'])  

of course, the idea is that I don’t know what the actual indexes are, so the intersection can be empty, and I get the concatenation of both DataFrames.

+4
source share
1 answer

You can combine by line, fill in the missing values ​​by 0 and by line:

>>> pd.concat([df1, df2], axis=1).fillna(0).sum(axis=1)
1    1
2    2
3    2
4    2
5    2
6    1
dtype: float64

If you want this as a DataFrame, just do

pd.DataFrame({
    'A': pd.concat([df1, df2], axis=1).fillna(0).sum(axis=1)})

(Also note that if you need to do this only for a specific series A, just use

pd.concat([df1.A, df2.A], axis=1).fillna(0).sum(axis=1)

)

+4
source

All Articles