Search code examples
pythondataframesumconcatenationinsert-update

Concatenate 2 dataframes (update) aggregating by sum


I have 2 dataframes, df1 and df2. Dataframe df2 must be concatenated with df1 with values in column 'bricks' which are not in df1 and if column value exists then update the volume data by adding the df1 'volume' with the df2 'volume'. The result should be sorted by 'bricks'.

Example:

df1 = [
    {'bricks':-3, 'volume':50},
    {'bricks':-2, 'volume':20},
    {'bricks':-1, 'volume':30}
]

df2 = [
    {'bricks':-1, 'volume':10},
    {'bricks':-2, 'volume':0},
    {'bricks':-3, 'volume':60},
    {'bricks':-4, 'volume':60},
    {'bricks':-5, 'volume':60},
]

Expected result:

df1 = [
    {'bricks':-5, 'volume':60},
    {'bricks':-4, 'volume':60},
    {'bricks':-3, 'volume':110},
    {'bricks':-2, 'volume':20},
    {'bricks':-1, 'volume':40}    
]

Solution

  • try this:

    pd.concat([df1,df2]).groupby('bricks').sum().reset_index()
    
        bricks  volume
    0   -5  60
    1   -4  60
    2   -3  110
    3   -2  20
    4   -1  40