在pandas数据框中添加新列

时间:2018-03-02 06:03:38

标签: python pandas numpy

我对python非常陌生,所以除了打字错误之外请其他。

我正在尝试根据不同列的特定条件在数据框中添加新列。所以它不是返回值,而是返回一个我刚刚传递的字符串。

我不知道为什么会这样,以及如何摆脱它。

附加屏幕enter image description here

vdx_access_table [ “Delivered_Engagements”] = vdx_access_table [ “Delivered_Engagements”]。astype(int)的

    vdx_access_table["Delivered_Impressions"]=vdx_access_table["Delivered_Impressions"].astype(int)

    choices_vdx_eng = vdx_access_table["Delivered_Engagements"]/vdx_access_table["BOOKED_IMP#BOOKED_ENG"]

    choices_vdx_cpcv = vdx_access_table["Delivered_Impressions"]/vdx_access_table["BOOKED_IMP#BOOKED_ENG"]

    vdx_access_table['Delivery%']=[choices_vdx_eng if x=='CPE' or x=='CPE+' else choices_vdx_cpcv for x in
                                   vdx_access_table['COST_TYPE']]

enter image description here

1 个答案:

答案 0 :(得分:1)

numpy.where使用条件isin

choices_vdx_eng=vdx_access_table["Delivered_Engagements"]/vdx_access_table['BOOKED_IMP#BOOKED_ENG'] 
choices_vdx_imp=vdx_access_table["Delivered_Impressions"]/vdx_access_table['BOOKED_IMP#BOOKED_ENG'] 

mask = vdx_access_table['COST_TYPE'].isin(['CPE','CPE+'])
vdx_access_table['Delivery%']= np.where(mask, choices_vdx_eng, choices_vdx_imp )

或者:

mask = vdx_access_table['COST_TYPE'].isin(['CPE','CPE+'])
vdx_access_table['Delivery%']= np.where(mask, 
                                        vdx_access_table["Delivered_Engagements"], 
                                        vdx_access_table["Delivered_Impressions"]) /vdx_access_table['BOOKED_IMP#BOOKED_ENG'] 

编辑:

df = pd.DataFrame({'Delivered_Engagements':[10,20,30,40,50],
                   'Delivered_Impressions':[5,4,8,7,3],
                   'BOOKED_IMP#BOOKED_ENG':[3,2,0,4,2],
                   'COST_TYPE':['CPE','CPE+','CPM','CPCV','AAA']})

df["Delivered_Engagements"]=df["Delivered_Engagements"].astype(int)
df["Delivered_Impressions"]=df["Delivered_Impressions"].astype(int)

eng = df["Delivered_Engagements"]/df["BOOKED_IMP#BOOKED_ENG"]
cpcv = df["Delivered_Impressions"]/df["BOOKED_IMP#BOOKED_ENG"]

mask1 = df["COST_TYPE"].isin(['CPE','CPE+'])
mask2 = df["COST_TYPE"].isin(['CPM','CPCV'])


df['Delivery%']=np.select([mask1, mask2], [eng, cpcv], default=0)

df['Delivery%']=df['Delivery%'].replace(np.inf,0)

print (df)
   BOOKED_IMP#BOOKED_ENG COST_TYPE  Delivered_Engagements  \
0                      3       CPE                     10   
1                      2      CPE+                     20   
2                      0       CPM                     30   
3                      4      CPCV                     40   
4                      2       AAA                     50   

   Delivered_Impressions  Delivery%  
0                      5   3.333333  
1                      4  10.000000  
2                      8   0.000000  
3                      7   1.750000  
4                      3   0.000000