在pandas数据框中添加新列

问题描述 投票:1回答:1

我对python几乎是新手,所以请除了拼写错误之外。

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

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

screenenter image description hereshot附加。

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

python pandas numpy
1个回答
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  
© www.soinside.com 2019 - 2024. All rights reserved.