TransWikia.com

Match two columns from two dataframe, if they match get the corresponding column value

Stack Overflow Asked by Aditya sharma on December 25, 2021

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.

2 Answers

You could use dictionaries, not super elegant but could be useful.

cluster_dict = df2.set_index('Cluster')['label'].to_dict()
df1.set_index('Col1')['Col2'].fillna(cluster_dict).reset_index()

   col1 col2
0     1   AA
1     2   AB
2     2   AB
3     2   AB
4     3   AC
5     3   AC

Answered by Mgaraz on December 25, 2021

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)

Answered by Hicham Zouarhi on December 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP