如何用Pandas优化 Dataframe 中的嵌套循环

3lxsmp7m  于 2023-02-07  发布在  其他
关注(0)|答案(1)|浏览(158)

我想优化迭代,因为完成操作需要很长时间,这是我的代码

the second table

l=0
for i in df_MailBox_Total.index : 
    if pd.isnull(df_MailBox_Total.at[i,'ReceivedOrgId']):
        for j in df_SentMail_Service.index :
            if df_MailBox_Total.at[i,'MailId'] == df_SentMail_Service.at[j,'MailID']:
               df_MailBox_Total.at[i,'ReceivedOrgId'] =df_SentMail_Service.at[j,'OrgID']
               l=l+1
               break;

nnsrf1az

nnsrf1az1#

IIUC,使用这个:

df_MailBox_Total = (df_MailBox_Total
                         .set_index('MailId')
                         .assign(ReceivedOrgId = lambda x: x["ReceivedOrgId"]
                                .fillna(df_SentMail_Service.set_index('MailID')['OrgID']))
                         .reset_index())

相关问题