Search code examples
pythonpython-3.xpandasdataframepandasql

overwrite and append pandas data frames on column value


I have a base dataframe df1:

id   name   count
1    a       10
2    b       20
3    c       30
4    d       40
5    e       50

Here I have a new dataframe with updates df2:

id   name   count
1    a       11
2    b       22
3    f       30
4    g       40

I want to overwrite and append these two dataframes on column name. for Eg: a and b are present in df1 but also in df2 with updated count values. So we update df1 with new counts for a and b. Since f and g are not present in df1, so we append them. Here is an example after the desired operation:

id   name   count
1    a       11
2    b       22
3    c       30
4    d       40
5    e       50
3    f       30
4    g       40

I tried df.merge or pd.concat but nothing seems to give me the output that I require.? Can any one


Solution

  • Using combine_first

    df2=df2.set_index(['id','name'])
    df2.combine_first(df1.set_index(['id','name'])).reset_index()
    Out[198]: 
       id name  count
    0   1    a   11.0
    1   2    b   22.0
    2   3    c   30.0
    3   3    f   30.0
    4   4    d   40.0
    5   4    g   40.0
    6   5    e   50.0