I have two dataframes, df1 and df2. They unique identifiers for each dataframe are 'ID' and 'Prop_Number'. I need to copy over the Num1, 2 and 3 columns from df1 into the corresponding columns in df2, 1_Num... But I'm not sure how to do a merge for more than column. I want to keep df2 as df2, not create a new df (because my real data has more columns in df2) that will remain as is.
cols1 = ['ID', 'Num1', 'Num2', 'Num3']
data1 = [['33', '.853', '9834', '234'],
['87', '.372', '2345', '843'],
['15', '1.234','742', '821'],
['92', '1.957', '1234', '123'],
['13', '.943', '8427', '493'],
['67', '.852', '3421', '439']
]
df1 = pd.DataFrame(data=data1, columns=cols1)
cols2 = ['Prop_Number', '1_Num', '2_Num', '3_Num']
data2 = [['87', '', '', ''],
['33', '', '', ''],
['67', '','', ''],
['13', '', '', ''],
['92', '', '', ''],
['15', '', '', '']
]
df2 = pd.DataFrame(data=data2, columns=cols2)
What I've tried is
df2['1_Num'] = np.where(df1['ID'] == df2['Prop_Number'], df1['Num1'],np.nan)