根据特定条件填充熊猫数据框列

时间:2019-08-08 16:30:28

标签: python pandas

我正在尝试根据某些条件填充df2['Revised Audience']列。同一列上需要满足多个条件。

因此,我试图使用df1['CombineColumn']df2['Campaign Name','Placement Name', 'Creative Name']中找到str.contrains的值,并使用df2['Revised Audience']的实际值填充df1['Actual']列。 / p>

我已经通过将df2['CombineColumn']df2['Actual']的值转换为列表创建了掩码。

mask_null = df2['Revised Audience'].isnull()
mask_campaign = df2['Campaign Name'].str.contains('|'.join(df1['CombineColumn'].tolist())
mask_placement = df2['Placement Name'].str.contains('|'.join(df1['CombineColumn'].tolist())
mask_creative = df2['Creative Name'].str.contains('|'.join(df1['CombineColumn'].tolist())

df2['Revised Audience'] = np.select([mask_null & mask_campaign], [?])
df2['Revised Audience'] = np.select([mask_null & mask_placement], [?])
df2['Revised Audience'] = np.select([mask_null & mask_creative], [?])

因此要代替'?',我希望将df1['Actual']值与每个df2['CombineColumn']

相对

df1:

Code   Actual           OtherPV SheetName CombineColumn
tgm    gen-targeted mass NaN        SA        SA~tgm
na     none              NaN        SA         SA~na
all    all               NaN        SA        SA~all

df2:
   Campaign Name            Placement Name                      Creative Name                       Revised Audience
TG~bhv_SA~tgm           RT~cpc_MD~h                         CV~ge_SA~dp_MG~ssh_TA~avn_CW~kevp   
SA~tgm_VV~dc            SA~dp_TS~ESiOS_TG~dmp13_RT~cpc_MD~h CE~loc_MG~ssh_TA~avn_CW~kevp    
RT~cpc_TG~bhv_SA~tgm    PB~fcbk_SA~dp                       CE~loc_MG~ssh_TA~avn_CW~kevp    
TG~bhv_SA~tgm           PB~fcbk_SA~dp                       CE~loc_MG~ssh_TA~avn_CW~kevp    
SA~tgm_VV~dc            PB~fcbk_SA~dp                       CE~loc_MG~ssh_TA~avn_CW~kevp    
RT~cpc_TG~bhv_SA~tgm    PB~fcbk_SA~dp                       CE~loc_MG~ssh_TA~avn_CW~kevp    
TG~bhv_SA~tgm           PB~fcbk_SA~dp                       CE~loc_MG~ssh_TA~avn_CW~kevp    
SA~tgm_VV~dc            PB~fcbk_SA~dp                       CE~loc_MG~ssh_TA~avn_CW~kevp    
RT~cpc_TG~bhv_SA~tgm    PB~fcbk_SA~dp                       CE~loc_MG~ssh_TA~avn_CW~kevp    
TG~bhv_SA~tgm           TG~notarg_SA~na_                    (not set)   
SA~tgm_VV~dc            TG~notarg_SA~na_                    (not set)   
RT~cpc_TG~bhv_SA~tgm    TG~notarg_SA~na_                    (not set)

0 个答案:

没有答案