I have two data Df1 and Df2.There are few rows in my Df1['Col2] which are empty. I want to fill this this blank row with corresponding value of Df2.
Df1
Col1 Col2
1 AA
2
2
2
3 AC
3 AC
Df2
Cluster label
1 AA
2 AB
3 AC
4 AD
Desired Output
Col1 Col2
1 AA
2 AB
2 AB
2 AB
3 AC
3 AC
I am trying the below code, but not getting the result :
Df1['Col2'] =np.where((Df2['Cluster']==Df1['Col1']),Df2['label'],'No label found')
I can not use merge function as I have some other constraints as well.
you can combine an apply with a condition on col2
df1{"col2"] = df1.apply(lambda x: df2[df2['Cluster'] == x ['col1']]['label'].tolist()[0] if x['col2'] is None else x['col2'], axis = 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With