0

this is my code to iterate two tables simultanous

for i in df_MailBox_SentService.index :
    val_s = df_MailBox_SentService.at[i,'MailId']
    for j in df_TBL_SentMail.index :
        if val_s == df_TBL_SentMail.at[j,'ID']:
            df_MailBox_SentService.at[i,'UnifiedMailNumber']=df_TBL_SentMail.at[j,'MailNbr']
       
Aymen Ragguem
  • 156
  • 2
  • 15
  • 1
    please fix your indentations – drum Feb 01 '23 at 15:10
  • 1
    you are assigning values to a position determined exclusively by `i` `df_MailBox_SentService.at[i,'UnifiedMailNumber']` but then you have an inner loop that will keep overwriting on that position every time the conditional is triggered (since `j` is not used to determine the position to write to). The loops don't make much sense to me, unless the conditional is guaranteed to trigger only once, in that case you should break the inner loop to improve performance – Sembei Norimaki Feb 01 '23 at 15:15
  • It looks like you are using pandas. In pandas you can join two dataframes, so you don't need to loop over them with for loops. If you need help with that, provide a full code example - including code to create the dataframes - so you can get help. Also useful, say how big the big tables are. – 576i Feb 01 '23 at 15:21
  • the two dataframe is 1 million record and 100k records – Aymen Ragguem Feb 01 '23 at 15:25
  • are you looking to https://stackoverflow.com/questions/37697195/how-to-merge-two-data-frames-based-on-particular-column-in-pandas-python – JonSG Feb 01 '23 at 16:03

1 Answers1

1

Try this:

mask = df_MailBox_SentService['MailId'] == df_TBL_SentMail['ID']
df_MailBox_SentService.loc[mask, 'UnifiedMailNumber'] = df_TBL_SentMail.loc[mask, 'MailNbr']
jjislam
  • 557
  • 3
  • 8