Reputation: 156
I've two data-frame, that I want to merge.
df1
sample,items,score
a,p1,0.9
b,p2,0.8
c,p3,0.6
df2
sample,items,score
d,p2,0.75
e,p3,0.7
f,p4,0.65
merge data-frame will be
sample,items,score,df_name
a,p1,0.9,df1
b,p2,0.8,df1
e,p3,0.7,df2
Condition:
Upvotes: 0
Views: 22
Reputation: 260300
You can use merge
to align the dataframes, then use boolean indexing for in place modification:
df3 = df1[['items']].merge(df2, on='items', how='left')[df1.columns]
df1.loc[df1['score'].lt(df3['score'])] = df3
modified df1
:
sample items score
0 a p1 0.9
1 b p2 0.8
2 e p3 0.7
To add the indicator:
df1['df_name'] = 'df1'
df1.loc[df1['score'].lt(df3['score'])] = df3.assign(df_name='df2')
output:
sample items score df_name
0 a p1 0.9 df1
1 b p2 0.8 df1
2 e p3 0.7 df2
Alternative using a temporary index:
df1 = df1.set_index('items')
df2 = df2.set_index('items')
df1['df_name'] = 'df1'
df1.loc[df1['score'].lt(df2['score'])] = df2.assign(df_name='df2')
df1 = df1.reset_index()
Upvotes: 1