Script 985: Percentage Budget Difference CC
Purpose
This script calculates the percentage difference between the current daily budget and the SBA recommended daily budget for campaigns.
To Elaborate
The Python script is designed to compute the percentage difference between the current daily budget and the recommended daily budget as suggested by the Structured Budget Allocation (SBA) for various campaigns. It processes data from a primary data source, calculates the budget difference percentage for each campaign, and formats the results for easy interpretation. The script ensures that the calculated percentage is rounded to two decimal places and appends a percentage sign for clarity. The final output includes key columns such as account, campaign, SBA strategy, and the calculated percentage budget difference, which are essential for budget analysis and decision-making.
Walking Through the Code
- Data Preparation
- The script begins by defining the primary data source and relevant columns, which include campaign details and budget information.
- It sets up the input DataFrame (
inputDf
) from the data source, which contains columns like ‘Campaign’, ‘Account’, ‘Rec. Daily Budget’, and ‘Current Daily Budget’.
- Calculation of Percentage Budget Difference
- The script calculates the percentage difference between the current and recommended daily budgets for each campaign.
- This is done by dividing the budget difference by the recommended daily budget and multiplying by 100.
- The resulting percentage is rounded to two decimal places for precision.
- Formatting and Output Preparation
- The script appends a ‘%’ sign to the calculated percentage values to clearly indicate that they are percentages.
- It selects the necessary columns for the output, including ‘Account’, ‘Campaign’, ‘SBA Strategy’, and the calculated ‘Percentage Budget Difference’.
- The columns are renamed appropriately for clarity in the output DataFrame.
- Output Display
- Finally, the script prints the first few rows of the output DataFrame to display the results, which include the account, campaign, SBA strategy, and the percentage budget difference.
Vitals
- Script ID : 985
- Client ID / Customer ID: 1306926629 / 60270083
- Action Type: Bulk Upload (Preview)
- Item Changed: Campaign
- Output Columns: Account, Campaign, Percentage Budget Difference
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
- Created by dwaidhas@marinsoftware.com on 2024-04-18 20:48
- Last Updated by dwaidhas@marinsoftware.com on 2024-04-18 21:41
> 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
39
40
41
42
43
44
45
##
## name: Percentage Budget Difference CC
## description:
## This Script helps us calculate the Percentage of the Budget Difference between Current Daily Budget and the SBA Recommended Daily Budget
##
## author: Dana Waidhas
## created: 2024-04-18
##
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
# primary data source and columns
inputDf = dataSourceDict["1"]
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_SBA_STRATEGY = 'SBA Strategy'
RPT_COL_REC_DAILY_BUDGET = 'Rec. Daily Budget'
RPT_COL_CURRENT_DAILY_BUDGET = 'Current Daily Budget'
RPT_COL_BUDGET_DIFFERENCE = 'Budget Difference'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_PERCENTAGE_BUDGET_DIFFERENCE = 'Percentage Budget Difference'
# Calculate percentage budget difference for each row
inputDf[BULK_COL_PERCENTAGE_BUDGET_DIFFERENCE] = (inputDf[RPT_COL_BUDGET_DIFFERENCE] / inputDf['Rec. Daily Budget']) * 100
# Round the percentage budget difference column to two decimal places
inputDf[BULK_COL_PERCENTAGE_BUDGET_DIFFERENCE] = inputDf[BULK_COL_PERCENTAGE_BUDGET_DIFFERENCE].round(2)
# Append '%' to the percentage budget difference values
inputDf[BULK_COL_PERCENTAGE_BUDGET_DIFFERENCE] = inputDf[BULK_COL_PERCENTAGE_BUDGET_DIFFERENCE].astype(str) + '%'
# Select required columns for output
outputDf = inputDf[[RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_SBA_STRATEGY, BULK_COL_PERCENTAGE_BUDGET_DIFFERENCE]]
# Rename columns
outputDf.columns = [BULK_COL_ACCOUNT, BULK_COL_CAMPAIGN, RPT_COL_SBA_STRATEGY, BULK_COL_PERCENTAGE_BUDGET_DIFFERENCE]
# Print output DataFrame
print(outputDf.head())
Post generated on 2024-11-27 06:58:46 GMT