简体   繁体   English

比较两个数据帧并根据掩码值将新列添加到数据帧

[英]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: 我正在根据那里的ID比较两个数据帧,然后使用以下代码合并它们:

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

df1 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 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 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: 现在基于输出,我正在比较_x和_y列的值并将其放入mask中:

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

mask o/p 面罩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: 基于此掩码值,我想提出以下条件:如果在mask [1]中出现false,它应该给我累积的“无匹配”结果,可以将其附加到输出结果中,例如:

        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: numpy.wherenumpy.all使用以进行快速矢量化解决方案:

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] : 感谢您的评论@markuscosinus,如果需要通过索引索引将掩码的第二个'column'进行比较-此处通过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: 将掩码转换为numpy数组或数据框,或者应该已经采用以下格式:

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 . 您可以将此列添加到df

Hope it helps ... :) 希望能帮助到你 ... :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个数据框,然后根据另一个将新列添加到其中一个数据框 - Compare two dataframes, and then add new column to one of the data frames based on the other 有没有办法比较包含浮点值的数据帧的两列并创建一个新列以基于它添加标签? - Is there a way to compare two columns of a dataframe containing float values and create a new column to add labels based on it? 比较两个数据帧,只获取索引和列名不匹配的值 - compare two data frames and get only non matching values with index and column names pandas dataframe python 如何基于列比较两个不同大小的数据框? - How to compare two data frames of different size based on a column? 比较 dataframe 中的数据,并在该列中添加具有修改数据值的新列 - Compare data within a dataframe and add new column with modified data values in that column 比较两个数据帧并将第二个 dataframe 中的新值添加到第一个数据帧 - Compare two dataframes and add new values in second dataframe to the first data-frame 如果该行出现在第二个 dataframe 中,您如何将两个数据框组合在两个值上并添加一个包含 boolean 的额外列? - How do you combine two data frames on two values and add an extra column containing a boolean for if the row appeared in the second dataframe? 如何基于一个数据框中的一列和第二个数据框中的两列合并两个数据框 - How to merge two data frames based on one column in one data frame and two column in second dataframe 按 python 中的每一列比较两个数据帧? - Compare two data frames by each column in python? 比较两个数据框pyspark中的列名 - Compare column names in two data frames pyspark
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM