3

Let say I have below 2 pandas dataframes

import pandas as pd
dat1 = pd.DataFrame({'A' : [1999, -11234], 'B' : [88888, -345], 'C' : [-7777, -8978], 'D' : [7777, -8978]})
dat2 = pd.DataFrame({'A' : [999, 1234], 'B' : [8888, -345], 'C' : [-7777, -8978]})
Collection = [dat1, dat2]

In my original case, I actually have an arbitrary number of dataframes in the list Collection. Now I want to add all such dataframes with element-by-element addition, based on row and column matches (if in a dataframe, if a column and/or row is missing then corresponding value will be assumed as 0 in such addition).

Is there any direct method/function available to achieve this?

0

2 Answers 2

3

You can use functools.reduce along with pd.DataFrame.add to add an arbitrary number of dataframes together:

from functools import reduce
import pandas as pd

dat1 = pd.DataFrame({'A': [1999, -11234], 'B': [88888, -345], 'C': [-7777, -8978], 'D': [7777, -8978]})
dat2 = pd.DataFrame({'A': [999, 1234], 'B': [8888, -345], 'C': [-7777, -8978]})
dat3 = pd.DataFrame({'A': [100, 200], 'B': [300, -400], 'C': [500, 600], 'D': [700, 800]})
collection = [dat1, dat2, dat3]
result = reduce(lambda x, y: x.add(y, fill_value=0), collection)

This will sum the DataFrames in the collection list, filling any missing values with 0.

Output result:

index A B C D
0 3098 98076 -15054 8477.0
1 -9800 -1090 -17356 -8178.0
Sign up to request clarification or add additional context in comments.

Comments

1

You could concat all the dataframes together, then use groupby and sum to get your desired result:

import pandas as pd

dat1 = pd.DataFrame({'A': [1999, -11234], 'B': [88888, -345], 'C': [-7777, -8978], 'D': [7777, -8978]})
dat2 = pd.DataFrame({'A': [999, 1234], 'B': [8888, -345], 'C': [-7777, -8978]})
dat3 = pd.DataFrame({'A': [100, 200], 'B': [300, -400], 'C': [500, 600], 'D': [700, 800]})
collection = [dat1, dat2, dat3]

pd.concat(collection).groupby(level=0).sum()

Output:

      A      B      C       D
0  3098  98076 -15054  8477.0
1 -9800  -1090 -17356 -8178.0

Thanks to @SashSinha for the data

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.