Script 883: Budget Capped Campaigns
Purpose
Python script to tag campaigns if the publisher cost for the prior day is 90% of the daily budget.
To Elaborate
The Python script aims to identify campaigns that have exceeded their budget by comparing the publisher cost for the prior day with the daily budget. If the publisher cost is greater than or equal to 90% of the daily budget and certain conditions are met (iDNE is less than 1.5 and lost impression share is greater than 10%), the campaign is tagged as “Budget Capped”. The script then outputs the modified input data frame, including the account, campaign, and budget alert status.
Walking Through the Code
- The script defines column constants for various columns in the input data frame.
- The ‘iDNE’ column in the input data frame is converted to numeric values.
- The script calculates the ratio of publisher cost to daily budget and adds it as a new column (‘Pub Cost / Budget’) in the input data frame.
- The ‘iDNE’ column is copied to a new column in the input data frame.
- The ‘Lost Imp Share’ column is copied to a new column in the input data frame.
- The script checks if the ‘Pub Cost / Budget’ is less than the threshold (90%) and sets the ‘Budget Alert’ column to an empty string for those rows.
- The script checks if the ‘Pub Cost / Budget’ is greater than or equal to the threshold, ‘iDNE’ is less than 1.5, and ‘Lost Imp Share’ is greater than 10%. If all conditions are met, the ‘Budget Alert’ column is set to “Budget Capped” for those rows.
- The modified input data frame is printed.
- The script selects specific columns (account, campaign, and budget alert) from the input data frame and assigns it to the output data frame.
Vitals
- Script ID : 883
- Client ID / Customer ID: 248500018 / 13095968
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Budget Alert
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: smalina@marinsoftware.com (smalina@marinsoftware.com)
- Created by Stephen Malina on 2024-03-29 17:37
- Last Updated by Stephen Malina on 2024-04-29 14:11
> See it in Action
Python Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#
# Tag campaign if Pub Cost for prior day is 90% of Daily Budget
#
#
# Author: Stephen Malina
# Date: 2024-03-27
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_PUBLISHER = 'Publisher'
RPT_COL_PUB_COST = 'Pub. Cost A$'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_DAILY_BUDGET = 'Daily Budget'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_BUDGET_ALERT = 'Budget Alert'
RPT_COL_iDNE = 'iDNE'
RPT_COL_LOST_IMP_SHARE = 'Lost Impr. Share (Budget) %'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_BUDGET_ALERT = 'Budget Alert'
# Convert the 'iDNE' column to numeric values
inputDf[RPT_COL_iDNE] = pd.to_numeric(inputDf[RPT_COL_iDNE], errors='coerce')
# Calculate the difference between daily budget and pub cost, include iDNE for consideration
inputDf['Pub Cost / Budget'] = inputDf[RPT_COL_PUB_COST] / inputDf[RPT_COL_DAILY_BUDGET]
inputDf['iDNE'] = inputDf[RPT_COL_iDNE]
inputDf['Lost Imp Share'] = inputDf[RPT_COL_LOST_IMP_SHARE]
# Output a "Budget Cap Alert" where daily spend is greater than 90% of daily budget and iDNE is less than 1.5
threshold = 0.90 # Adjust this threshold as needed
inputDf.loc[inputDf['Pub Cost / Budget'] < threshold, BULK_COL_BUDGET_ALERT] = ""
inputDf.loc[(inputDf['Pub Cost / Budget'] >= threshold) & (inputDf['iDNE'] < 1.5) & (inputDf['Lost Imp Share'] > 10), BULK_COL_BUDGET_ALERT] = "Budget Capped"
# Print the modified inputDf
print(inputDf)
cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, BULK_COL_BUDGET_ALERT]
outputDf = inputDf[cols]
Post generated on 2024-05-15 07:44:05 GMT