Script 691: NOTICE Campaign Strategy Associations Campaign Name Dimension Auto Tagging School Program
Purpose:
The Python script automates the process of tagging campaigns with combined school and program dimensions and assigns an SBA strategy based on these tags.
To Elaborate
The script is designed to enhance campaign data by automatically generating a new tag, School_Program
, which combines the values from the ‘School’ and ‘Program’ columns. This tag is then used to assign an SBA strategy if it is not already specified. The script processes input data, ensuring that each campaign has a meaningful tag and strategy, which is crucial for effective campaign management and reporting. It also filters out any rows where both the School_Program
, SBA Strategy
, and Campaign ID
are missing, ensuring that only relevant and complete data is retained for further analysis or action.
Walking Through the Code
- Initialization and Input Handling
- The script begins by defining the necessary input and output column names for processing campaign data.
- It retrieves the primary data source and initializes an output DataFrame by copying the input data to maintain the same structure.
- Generating the
School_Program
Tag- The script applies a lambda function to each row of the DataFrame to create a
School_Program
tag by concatenating the ‘School’ and ‘Program’ values with an underscore, provided both values are not null.
- The script applies a lambda function to each row of the DataFrame to create a
- Assigning SBA Strategy
- It updates the
SBA Strategy
column by assigning theSchool_Program
tag to it if theSBA Strategy
is not already defined.
- It updates the
- Data Cleaning
- The script filters out rows where the
School_Program
,SBA Strategy
, andCampaign ID
are all missing, ensuring only complete and actionable data remains.
- The script filters out rows where the
- Output Verification
- Finally, it checks if the output DataFrame is empty and prints a sample of the data if it contains any rows, otherwise, it indicates that the DataFrame is empty.
Vitals
- Script ID : 691
- Client ID / Customer ID: 1306926629 / 60270083
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, School_Program, SBA Strategy, Campaign ID
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
- Created by dwaidhas@marinsoftware.com on 2024-02-09 19:49
- Last Updated by dwaidhas@marinsoftware.com on 2024-12-13 20:38
> 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
##
## name: Campaign Name Dimension Auto Tagging: School_Program
## description:
## Combine Dimensions columns 'School' and 'Program' into column 'School_Program'
## Assign SBA Strategy using the School_Program if available.
##
## author: Dana Waidhas
## created: 2024-02-09
##
# Input columns
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_SCHOOL = 'School'
RPT_COL_PROGRAM = 'Program'
RPT_COL_SCHOOL_PROGRAM = 'School_Program'
RPT_COL_SBA_STRATEGY = 'SBA Strategy'
RPT_COL_CAMPAIGN_ID = 'Campaign ID'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
# Output columns
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_SCHOOL = 'School'
BULK_COL_PROGRAM = 'Program'
BULK_COL_SCHOOL_PROGRAM = 'School_Program'
BULK_COL_SBA_STRATEGY = 'SBA Strategy'
BULK_COL_CAMPAIGN_ID = 'Campaign ID'
BULK_COL_CAMPAIGN_STATUS = 'Campaign Status'
# Primary data source and columns
inputDf = dataSourceDict["1"]
# Output DataFrame initialization
outputDf = inputDf.copy() # Ensures outputDf has the same columns as inputDf
# Generate School_Program tag
outputDf['School_Program'] = outputDf.apply(
lambda row: f"{row[RPT_COL_SCHOOL]}_{row[RPT_COL_PROGRAM]}"
if pd.notna(row[RPT_COL_SCHOOL]) and pd.notna(row[RPT_COL_PROGRAM])
else None,
axis=1
)
# Assign SBA Strategy using the School_Program if available
outputDf[RPT_COL_SBA_STRATEGY] = outputDf[RPT_COL_SBA_STRATEGY].where(
outputDf[RPT_COL_SBA_STRATEGY].notna(), outputDf['School_Program']
)
# Drop rows where both School_Program, SBA Strategy, and Campaign ID are blank
outputDf = outputDf[
outputDf[[BULK_COL_SCHOOL_PROGRAM, RPT_COL_SBA_STRATEGY, RPT_COL_CAMPAIGN_ID]].notna().any(axis=1)
]
if not outputDf.empty:
print("outputDf example", outputDf.head().to_string(index=False))
else:
print("Empty outputDf")
Post generated on 2025-03-11 01:25:51 GMT