簡體   English   中英

比較兩個數據幀並根據掩碼值將新列添加到數據幀

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

我正在根據那里的ID比較兩個數據幀,然后使用以下代碼合並它們:

        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  

現在基於輸出,我正在比較_x和_y列的值並將其放入mask中:

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

面罩o / p

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

基於此掩碼值,我想提出以下條件:如果在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 

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

感謝您的評論@markuscosinus,如果需要通過索引索引將掩碼的第二個'column'進行比較-此處通過mask[:, 1]

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

將掩碼轉換為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]])

然后下面的代碼為您提供所需的列:

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

您可以將此列添加到df

希望能幫助到你 ... :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM