Script 1497: Assign 'Unassigned' Campaigns to Strategy
Purpose
The Python script assigns a strategy to campaigns that are currently marked as ‘Unassigned’ based on a specific naming pattern.
To Elaborate
The script is designed to automate the process of assigning a strategy to marketing campaigns that do not have one. It processes a dataset containing campaign information and checks each campaign’s name against a predefined pattern. If the campaign name matches the pattern, the first ten characters of the name are used as the strategy. If it does not match, the strategy is set to ‘Unassigned’. This ensures that all campaigns have a strategy assigned, which is crucial for structured budget allocation (SBA) and effective campaign management. The script is particularly useful for marketing teams who need to quickly organize and categorize campaigns based on their naming conventions.
Walking Through the Code
- Data Preparation
- The script begins by defining the primary data source and relevant column names for processing. It creates a copy of the input DataFrame to work on, ensuring the original data remains unchanged.
- Pattern Matching and Strategy Assignment
- A regular expression pattern is defined to identify campaign names that follow a specific format (‘XXXX_XXXXX’). The script applies a lambda function to each campaign name, checking if it matches the pattern. If it does, the first ten characters are assigned as the strategy; otherwise, the strategy is set to ‘Unassigned’.
- Verification
- The script concludes by printing the first few rows of the modified DataFrame to verify the changes, allowing users to confirm that the strategy assignment has been executed correctly.
Vitals
- Script ID : 1497
- Client ID / Customer ID: 1306926629 / 60270083
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Campaign ID, Strategy
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
- Created by dwaidhas@marinsoftware.com on 2024-11-07 21:20
- Last Updated by dwaidhas@marinsoftware.com on 2024-11-08 14:36
> 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
##
## name: Assign 'Unassigned' Campaigns to Strategy
## description:
##
##
## author: Dana Waidhas
## created: 2024-11-07
##
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_ID = 'Campaign ID'
RPT_COL_STRATEGY = 'Strategy'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_SCHOOL_PROGRAM = 'School Program'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_STRATEGY = 'Strategy'
BULK_COL_CAMPAIGN_ID = 'Campaign ID'
outputDf = inputDf.copy() # Create a copy of the input DataFrame for output
# Regular expression pattern for 'XXXX_XXXXX' format
pattern = r'^\w{4}_\w{5}'
# Apply conditional assignment to Strategy field
outputDf[BULK_COL_STRATEGY] = outputDf[RPT_COL_CAMPAIGN].apply(
lambda x: x[:10] if re.match(pattern, x[:10]) else 'Unassigned'
)
# Display result for verification
print(tableize(outputDf.head()))
Post generated on 2024-11-27 06:58:46 GMT