简体   繁体   中英

compare two data frames and add new column to dataframe based on mask values

I am comparing two data frames based on there ids and then merging them using below code:

        df = df1.merge(df2, on=id, suffixes=('_x','_y'))    

df1

        name  age  id  salary  
    0   Smith   30   2    2000  
    1     Ron   24   3   30000  
    2    Mike   35   4   40000  
    3    Jack   21   5    5000  
    4  Roshan   20   6   60000  
    5   Steve   45   8    8000  
    6   Peter   28   1    1000  

df2

       name  age  salary  id  
    0  Peter   32   10000   1  
    1  Smith   30    1500   2  
    2    Ron   24    7000   3  
    3   Mike   35   20000   4  
    4   Jack   21    5000   5  
    5  Cathy   20    9000   6  
    6  Steve   45   56000   8  

o/p

            name_x  age_x  id  salary_x name_y  age_y  salary_y  
        0   Smith     30   2      2000  Smith     30      1500  
        1     Ron     24   3     30000    Ron     24      7000  
        2    Mike     35   4     40000   Mike     35     20000  
        3    Jack     21   5      5000   Jack     21      5000  
        4  Roshan     20   6     60000  Cathy     20      9000  
        5   Steve     45   8      8000  Steve     45     56000  
        6   Peter     28   1      1000  Peter     32     10000  

Now based on the output i am comparing _x and _y column values and putting it into mask:

        mask = df[cols + '_x'].values == df[cols + '_y'].values    
        print(mask)    

mask o/p

    [[ True  True False]  
    [ True  True False]  
    [ True  True False]  
    [ True  True  True]  
    [ True False False]  
    [ True  True False]  
    [False  True False]]  

Based on this mask value i want to put condition that if false is present at let say mask[1] it should give me cumulative result of 'No MAtch' that i can append to my output results like:

        name_x  age_x  id  salary_x name_y  age_y  salary_y  new_column  
    0   Smith     30   2      2000  Smith     30      1500  No Match  
    1     Ron     24   3     30000    Ron     24      7000  No Match  
    2    Mike     35   4     40000   Mike     35     20000  No Match  
    3    Jack     21   5      5000   Jack     21      5000  MAtch  
    4  Roshan     20   6     60000  Cathy     20      9000  No Match  
    5   Steve     45   8      8000  Steve     45     56000  No Match  
    6   Peter     28   1      1000  Peter     32     10000  No Match
matches = ['Match' if x else 'No Match' for x in np.all(mask, axis = -1)]

将为您提供'Match''No Match'值的数组,您可以通过以下方式将其添加到数据框中:

df['newColumnName'] = matches 

Use numpy.where with numpy.all for fast vectorized solution:

mask = df[cols + '_x'].values == df[cols + '_y'].values  

df['new_column'] = np.where(np.all(mask, axis=1) , 'Match','No Match')
print (df)
   name_x  age_x  id  salary_x name_y  age_y  salary_y new_column
0   Smith     30   2      2000  Smith     30      1500   No Match
1     Ron     24   3     30000    Ron     24      7000   No Match
2    Mike     35   4     40000   Mike     35     20000   No Match
3    Jack     21   5      5000   Jack     21      5000      Match
4  Roshan     20   6     60000  Cathy     20      9000   No Match
5   Steve     45   8      8000  Steve     45     56000   No Match
6   Peter     28   1      1000  Peter     32     10000   No Match

Thanks for comment @markuscosinus, if need compare by second 'column' of mask seelct by indexing - here by mask[:, 1] :

df['new_column'] = np.where(mask[:, 1] , 'Match','No Match')

Convert the mask to numpy array or a dataframe, or it should already be in this format:

mask = pd.DataFrame([[ True, True, False],
                     [ True, True, False],
                     [ True, True, False],
                     [ True, True, True],
                     [ True, False, False],  
                     [ True, True, False],  
                     [False, True, False]])

And then the following code give you the column you want:

mask.apply(sum, axis=1).apply(lambda x: 'Match' if x==3 else 'No Match')

You can add this column to df .

Hope it helps ... :)

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