I have two different dataframes (df1, df2) with completely different shapes: df1: (64, 6); df2: (564, 9).
df1 contains a column (df1.objectdesc) which has values (strings) that can also be found in a column in df2 (df2.objdescription). As the two dataframes have different shapes I have to work with .isin()
to get the matching values. I then would like to get a third value from a different column in df2 (df2.idname) from exactly those rows which match and add them to df1 - this is where I struggle.
example datasets:
df1
Content objectdesc TS_id
0 sdrgs 1_OG.Raum45 55
1 sdfg 2_OG.Raum23 34
2 psdfg GG.Raum12 78
3 sdfg 1_OG.Raum98 67
df2:
Numb_val object_count objdescription min idname
0 463 9876 1_OG_Raum76 1 wq19
1 251 8324 2_OG.Raum34 9 zt45
2 456 1257 1_OG.Raum45 4 bh34
3 356 1357 2_OG.Raum23 3 if32
4 246 3452 GG.Raum12 5 lu76
5 345 8553 1_OG.Raum98 8 pr61
expected output:
Content objectdesc TS_id idname
0 sdrgs 1_OG.Raum45 55 bh34
1 sdfg 2_OG.Raum23 34 if32
2 psdfg GG.Raum12 78 lu76
3 sdfg 1_OG.Raum98 67 pr61
This is my code so far:
def get_id(x, y):for values in x,y:if x['objectdesc'].isin(y['objdescription']).any() == True:return y['idname']df1['idname'] = get_id(df1, df2)
This unfortunately only provides the values of df2['idname']
starting from index 0, instead of really giving me the values from the rows which match.
Any help is appreciated. Thank you!