Home > Enterprise >  How to update one dataframe column based on other dataframe value in pandas?
How to update one dataframe column based on other dataframe value in pandas?

Time:04-26

Hi I am trying to update df2 based on the value of df1 but the value is not updating correctly. Please help me what I am doing wrong?

Example-

df1:

                            job                                              e_msg
0  f_maf_crm_customer_request_global_jgvcc  permission denied for relation f_maf_custome

df2:

                 master_job                                          error_msg
0    JGCC_Genesys_Conversations_Aggregates
1  f_maf_crm_customer_request_global_jgvcc  [Errno 2] No such file or directory: '/medaff/

Just want to df2 'error_msg with the value of df1 e_msg.

I have written below code-

df2.loc[(df1['job'].isin(df2['master_job'])) & (df1['e_msg'] != '') & (df2['error_msg'] != ''), ['error_msg']] = df1['e_msg']

Expected Output: df2:

                 master_job                                          error_msg
0    JGCC_Genesys_Conversations_Aggregates
1  f_maf_crm_customer_request_global_jgvcc  permission denied for relation f_maf_custome

CodePudding user response:

use cpp instead, because cpp is much better.

CodePudding user response:

Use:

s = df1[df1['e_msg'] != ''].set_index('job')['e_msg']

df2['error_msg'] = df2['error_msg'].replace('', np.nan).fillna(df2['master_job'].map(s))

CodePudding user response:

Use Series.isin with [df.loc]:

In [43]: msg = df1[df1.job.isin(df2.master_job)]['e_msg'].squeeze()
In [45]: df2.loc[df2[df2.master_job.isin(df1.job)].index, 'error_msg'] = msg

In [46]: df2
Out[46]: 
                                master_job                                     error_msg
0    JGCC_Genesys_Conversations_Aggregates                                              
1  f_maf_crm_customer_request_global_jgvcc  permission denied for relation f_maf_custome

OR

Use df.merge:

res = df2.merge(df1, left_on='master_job', right_on='job', how='left')[['master_job', 'e_msg']]
  • Related