DKM
DKM

Reputation: 1801

Add new column in pandas data frame

I'm pretty much new to python so please except typos and all.

I am trying to add the new column in data frame based on the certain condition of the different column. so instead of returning values, it is returning a string which I'm just passing.

I don't know why it's happening and how to get rid of from that.

screenenter image description hereshot attached.

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

Upvotes: 1

Views: 156

Answers (1)

jezrael
jezrael

Reputation: 862511

Use numpy.where with condition by 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 )

Or:

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'] 

EDIT:

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  

Upvotes: 1

Related Questions