Bimbo
Bimbo

Reputation: 69

pandas create new column after merge

Thank you in advance for taking the time to help me

I have 2 dataframes: 1 - dataframe with notifications that i want to send to users

notifications_to_send = pd.DataFrame({'user_id': ['201', '207', '223', '212', '112', '311'],
                   'id_notification': ['1', '1', '1', '1', '1', '1'],
                     'email':['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]']
                     })
   user_id   id_notification    email
0   201             1           [email protected]
1   207             1           [email protected]
2   223             1           [email protected]
3   212             1           [email protected]
4   112             1           [email protected]
5   311             1           [email protected]

2 - dataframe with info about users who set option to don't limit the quantity of sending notification to them

unlimited_notifications = pd.DataFrame({'user_id': ['201', '212'],
                   'id_notification': ['1', '2']})
  user_id   id_notification
0   201           1
1   212           2

so I want to add column 'send_push' to first dataframe, that will determine that push will be sent if user set unlimited option I thought about merge 2 dataframes on user_id and id_notification

notifications_to_send.merge(unlimited_notifications, on=['user_id','id_notification'], how='left')

, and for row which fits with this condition 'send_push' will be set on True, if condition don't execute then set it on Null, so it would look like this:

  user_id    id_notification    email             send_push
0   201             1           [email protected]    True
1   207             1           [email protected]    null
2   223             1           [email protected]    null
3   212             1           [email protected]    null
4   112             1           [email protected]    null
5   311             1           [email protected]    null

so the questions is how to add new column while merge or maybe there is any other way to make it? Compare two dataframes?

Upvotes: 1

Views: 1120

Answers (2)

Hanalia
Hanalia

Reputation: 197

You can just add send_push column to the 2nd dataframe before merging.

When you merge, only the rows where userid and id_notification match the first dataframe will return True.

enter image description here

Upvotes: 1

Anurag Dabas
Anurag Dabas

Reputation: 24324

Perform left merge then compare the 'id_notification' of both df's and finally assign values according to that:

import numpy as np

notifications_to_send=notifications_to_send.merge(unlimited_notifications,how='left',on='user_id',suffixes=('','_y'))
notifications_to_send['send_push']=np.where(notifications_to_send.eval("id_notification==id_notification_y"),True,pd.NA)
notifications_to_send=notifications_to_send.drop(columns='id_notification_y')

OR

without np.where():

notifications_to_send=notifications_to_send.merge(unlimited_notifications,how='left',on='user_id',suffixes=('','_y'))
m=notifications_to_send['id_notification'].eq(notifications_to_send.pop('id_notification_y'))
notifications_to_send.loc[m,'send_push']=True
notifications_to_send.loc[~m,'send_push']=pd.NA

Upvotes: 3

Related Questions