简体   繁体   中英

compare two dataframes and append the data in python pandas

I've a sample dataframe df1

id  user_id     name             email       
1     1        John         John@example.com
2     2        Alves        alves@example.com
3     3        Kristein     kristein@example.com
4     4        James        james@example.com

The second dataframe df2

id      user       user_email_1            user_email_2         status
1      Sanders     sanders@example.com                          active
2      Alves       alves111@example.com   alves@example.com     active
3      Micheal     micheal@example.com                          active
4      James       james@example.com                            delete

How can I add the status data from df2 to df1 if

user_id of df1 and id of df2

name of df1 and user of df2

email of df1 matches with user_email_1 or user_email_2 of df2 matches and drops the not matched records?

Desired Result df1 :

id   user_id    name       email                status
2      2        Alves     alves@example.com     active
4      4        James     james@example.com     delete

For example:

As alves@example.com from df1 matches with user_email_2 , it appended the status data.

你应该使用合并

df1.merge(df2.reset_index(), how='inner', left_on=['name', 'email', 'id'], right_on=['user', 'user_email', 'index'])

Rearrange your dataframe df2 to get only one user_email column then merge the two dataframes and keep wanted columns:

df2 = df2.set_index(['id', 'user', 'status']).stack() \
         .rename('user_email').reset_index()

out = pd.merge(df1, df2, left_on=['user_id', 'name', 'email'],
                         right_on=['id', 'user', 'user_email'],
                         suffixes=('', '2')) \
          [['id', 'user_id', 'name', 'email', 'status']]
>>> out

   id  user_id   name              email  status
0   2        2  Alves  alves@example.com  active
1   4        4  James  james@example.com  delete

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