I have 2 dataframes:
df1 =
item shop revenue
0 1 0 0.0
1 2 1 0.0
2 3 2 0.0
df2 =
item shop revenue
0 1 0 33
1 2 1 244
2 3 2 124
3 4 3 26
I want to map the value of revenue from df2 based on the item and shop equality. I do this in a painful way first by combining two columns and using them as index. Then I map the value and finally I drop the extra columns.
df1['new_id']=df1["shop"].astype(str) +"_"+ df1["item"].astype(str)
df2['new_id']=df2["shop"].astype(str) +"_"+ df2["item"].astype(str)
df1 = df1.set_index("new_id")
df1.update(df2.set_index("new_id"))
df1 = df1.reset_index()
df1 = df1.drop(['new_id'],axis=1)
df2 = df2.drop(['new_id'],axis=1)
df1 =
item shop revenue
0 1 0 33.0
1 2 1 244.0
2 3 2 124.0
There must a better and more concise way of doing this with a simpler code. Could you please advise me on a better approach?
df1.revenueonly0values?