ilya
ilya

Reputation: 129

Merge two Dataframes with same columns with overwrite

I have dataframe like this:

    df = pd.DataFrame({"flag":["1","0","1","0"], 
     "val":["111","111","222","222"], "qwe":["","11","","12"]})

It gives:

   flag qwe val
0    1      111
1    0  11  111
2    1      222
3    0  12  222

Then i am filtering first dataframe like this:

    dff = df.loc[df["flag"]=="1"]
    **was:**
    dff.loc["qwe"] = "123"
    **edited:** (setting all rows in column "qwe" to "123")
    dff["qwe"] = "123" 

And now i need to merge/join df and dff in such a way to get:

   flag qwe val
0    1  123 111
1    0  11  111
2    1  123 222
3    0  12  222

Adding changes in 'qwe' from dff only if df value is empty.

Something like this:

pd.merge(df, dff, left_index=True, right_index=True, how="left")

gives

    flag_x qwe_x val_x flag_y qwe_y val_y
0      1         111      1         111
1      0    11   111    NaN   NaN   NaN
2      1         222      1         222
3      0    12   222    NaN   NaN   NaN

so, after that i need to drop flag_y, val_y, rename _x columns and merge manually qwe_x and qwe_y. But is there any way to make it easier?

Upvotes: 0

Views: 1015

Answers (2)

ilya
ilya

Reputation: 129

After edited changes, for me works this code:

c1 = dff.combine_first(df)

It produces:

   flag  qwe val
0    1  123  111
1    0   11  111
2    1  123  222
3    0   12  222

Which is exactly i was looking for.

Upvotes: 0

eva-vw
eva-vw

Reputation: 670

pd.merge has an on argument that you can use to join columns with the same name in different dataframes.

Try:

pd.merge(df, dff, how="left", on=['flag', 'qwe', 'val'])

However, I don't think you need to do that at all. You can produce the same result using df.loc to conditionally assign a value:

df.loc[(df["flag"] == "1") & (df['qwe'].isnull()), 'qwe'] = 123

Upvotes: 2

Related Questions