Reputation: 554
I have 2 dataframes. One first is my reference
df_ref
ID REF VALUE
A 1 12
A 2 36
A 3 95
B 1 54
B 2 67
B 3 81
C 1 89
C 2 123
C 3 14
And the second is my restricted :
df_restrict
ID V1 V2
A 1 2
B 3 2
C 2 1
What I want is to replace V1 and V2 in df_restrict
by the values corresponding to the (ID, REF)
from df_ref
df_restrict
ID V1 V2
A 12 36
B 81 67
C 123 89
Thanks in advance
Upvotes: 2
Views: 83
Reputation: 323226
Try something
out = df_restrict.set_index('ID').T.replace(df_ref.pivot(*df_ref.columns).T).T
Out[188]:
V1 V2
ID
A 12 36
B 81 67
C 123 89
Upvotes: 1
Reputation: 71689
We can pivot df_ref
to reshape, then set the index of df_restrict
to ID
, then transpose and replace the the values from the reshaped df_ref
r = df_ref.pivot('REF', 'ID', 'VALUE')
df_restrict.set_index('ID').T.replace(r).T
V1 V2
ID
A 12 36
B 81 67
C 123 89
Upvotes: 5
Reputation: 10624
Here is my suggestion:
df_restrict['V1']=df_restrict.merge(df_ref, left_on=['ID', 'V1'], right_on=['ID', 'REF'])['VALUE']
df_restrict['V2']=df_restrict.merge(df_ref, left_on=['ID', 'V2'], right_on=['ID', 'REF'])['VALUE']
>>> print(df_restrict)
ID V1 V2
0 A 12 36
1 B 81 67
2 C 123 89
Upvotes: 1
Reputation: 59539
melt
the second DataFrame so you can merge
the values on both columns, ['ID', 'REF']
, and then pivot
back to your original shape.
df1 = (df_restrict.melt(id_vars=['ID'], value_name='REF')
.merge(df_ref, how='left')
.pivot(index='ID', columns='variable', values='VALUE')
.rename_axis(columns=None))
# V1 V2
#ID
#A 12 36
#B 81 67
#C 123 89
Upvotes: 3