简体   繁体   中英

How two panda data frames with same column values can be merged to form the third data frame that shows the difference of the values

dataframe: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   32   1    1000

dataframe:df2

    name  age  salary  id
0  Peter   28   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

df1 and df2 To be merged on ID's.Please not that ID's are same in both df1 and df2 but id order is different.df3 needs to be created like below-

     name       age    id    salary
0   Smith        30     2    2000|1500
1     Ron        24     3    30000|7000
2    Mike        35     4    40000 |20000
3    Jack        21     5    5000
4  Roshan|Cathy  20     6    60000|9000
5   Steve        45     8    8000|56000
6   Peter        32|28  1    1000|10000

I am planning to put the above output to excel sheet using to_excel functionality. Before that i want to add one more extra column to this data frame which says 'match and 'mismatch' . Logic would be if any one of the row showing minimum of one difference value result should be mismatch else match.I am mocking the output below something like this-

id age name salary Result 0 2 30 Smith 2000|1500 Mismatch 1 3 24 Ron 30000|7000 Mismatch 3 5 21 Jack 5000 Match 4 6 20 Roshan|Cathy 60000|9000 MisMatch 5 8 45 Steve 8000|56000 MisMatch 6 1 32|28 Peter 1000|10000 MisMatch

What can i use for achieving such result

Use merge first and then join columns by condition with numpy.where , last filter only columns by df1.columns :

cols = df1.columns.difference(['id'])
df = df1.merge(df2, on='id', suffixes=('','_'))

s = df[cols].astype(str) + '|' + df[cols + '_'].astype(str).values
mask = df[cols].values != df[cols + '_'].values

arr =  np.where(mask, s, df[cols].astype(str))

df = df1[['id']].join(pd.DataFrame(arr, columns=cols))
print (df)
   id    age          name       salary
0   2     30         Smith    2000|1500
1   3     24           Ron   30000|7000
2   4     35          Mike  40000|20000
3   5     21          Jack         5000
4   6     20  Roshan|Cathy   60000|9000
5   8     45         Steve   8000|56000
6   1  32|28         Peter   1000|10000

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