简体   繁体   中英

Python/pandas: Find matching values from two dataframes and return third value

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!

may be try this:

df1.merge(df2, left_on='objectdesc', right_on='objdescription')[['Content', 'objectdesc', 'TS_id', 'idname']]

reference:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.merge.html

You can merge the two.

from io import StringIO

import pandas as pd

df_1_str = \
    '''
    Content    objectdesc    TS_id
    sdrgs      1_OG.Raum45   55
    sdfg       2_OG.Raum23   34
    psdfg      GG.Raum12     78
    sdfg       1_OG.Raum98   67
    '''

df_2_str = \
    '''
    Numb_val    object_count     objdescription    min   idname
    463         9876             1_OG_Raum76       1     wq19
    251         8324             2_OG.Raum34       9     zt45
    456         1257             1_OG.Raum45       4     bh34
    356         1357             2_OG.Raum23       3     if32
    246         3452             GG.Raum12         5     lu76
    345         8553             1_OG.Raum98       8     pr61
    '''

df_1 = pd.read_csv(StringIO(df_1_str), header=0, delim_whitespace=True)

df_2 = pd.read_csv(StringIO(df_2_str), header=0, delim_whitespace=True)

df_3 = df_1.merge(df_2[['objdescription', 'idname']], left_on='objectdesc',
                  right_on='objdescription').drop('objdescription', axis='columns')

Contents of df_3 :

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM