'Pandas create new column referencing another column in another data frame [duplicate]

I am attempting to add another column to df2 referencing df1

>>> df1
    col1  col2
0     A   Name A
1     B   Name B


>>> df2
    col1  col2
0     A   12.3
1     B   34.0
2     A   103.2

so that col1 in df2 is used to get the col2 value from df1, i.e. to produce:

>>> df2
    col1  col2   col3 
0     A   12.3   Name A
1     B   34.0   Name B
2     A   103.2  Name A

(I can do it with creating an empty column in df2 and then iterating over rows such as for n in range(0,df.shape[0])…then iloc, but apparently this is bad practice.)



Sources

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

Source: Stack Overflow

Solution Source