Script 985: Percentage Budget Difference CC
Purpose
Calculates the percentage of the budget difference between the current daily budget and the SBA recommended daily budget.
To Elaborate
This Python script solves the problem of calculating the percentage difference between the current daily budget and the SBA recommended daily budget for each row in a given input DataFrame. The script performs the following steps:
- Retrieves the necessary columns from the input DataFrame.
- Calculates the percentage budget difference by dividing the budget difference by the recommended daily budget and multiplying by 100.
- Rounds the percentage budget difference to two decimal places.
- Appends ‘%’ to the percentage budget difference values.
- Selects the required columns for the output DataFrame.
- Renames the columns in the output DataFrame.
- Prints the head of the output DataFrame.
Walking Through the Code
- The script starts by importing the necessary libraries and defining the required constants.
- The input DataFrame is retrieved from the primary data source.
- The script defines the columns needed for the calculations and the output.
- The percentage budget difference is calculated by dividing the budget difference by the recommended daily budget and multiplying by 100.
- The percentage budget difference column is rounded to two decimal places.
- The ‘%’ symbol is appended to the percentage budget difference values.
- The required columns for the output DataFrame are selected.
- The columns in the output DataFrame are renamed.
- The head of the output DataFrame is printed.
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-05-15 07:44:05 GMT