Reputation: 19486
I have two dataframes, df_original
and df_update
, both with multiindex.
I want to update df_original
with the values of df_update
where keys match, and append any new data from df_update
to df_original
.
How do I do this?
(I've tried df.update
and df.merge
but can't seem to find a clean solution).
Upvotes: 1
Views: 85
Reputation:
Your description matches with the description of the combine_first
method:
Combine two DataFrame objects and default to non-null values in frame calling the method. Result index columns will be the union of the respective indexes and columns
Parameters: other : DataFrame
Returns: combined : DataFrame
idx = pd.MultiIndex.from_tuples([('a', 'x'), ('a', 'y'), ('b', 'x')])
df1 = pd.DataFrame([1, 2, 3], index=idx)
df1
Out:
0
a x 1
y 2
b x 3
idx = pd.MultiIndex.from_tuples([('a', 'y'), ('b', 'x'), ('b', 'y')])
df2 = pd.DataFrame([4, 5, 6], index=idx)
df2
Out:
0
a y 4
b x 5
y 6
df1.combine_first(df2)
Out:
0
a x 1.0
y 2.0
b x 3.0
y 6.0
Upvotes: 1
Reputation: 1759
print(df_original.merge(df_update,how="outer"))
I think this would work
Upvotes: 0