'How to pull a specific value from one dataframe into another?

I have two dataframes

enter image description here

How would one populate the values in bold from df1 into the column 'Value' in df2?



Solution 1:[1]

Use melt on df1 before merge your 2 dataframes

tmp = df1.melt('Rating', var_name='Category', value_name='Value2')
df2['Value'] = df2.merge(tmp, on=['Rating', 'Category'])['Value2']
print(df2)

# Output
    Category Rating  Value
0  Hospitals    A++    2.5
1  Education     AA    2.1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien