Script 535: Assigned Brand Local Emergency Dimensions
Purpose
The Python script assigns dimension values to campaigns based on whether they are categorized as “Brand,” “Local,” or “Emergency.”
To Elaborate
The script processes a dataset of marketing campaigns to categorize each campaign into one of three dimensions: “Brand,” “Local,” or “Emergency.” This categorization is based on the presence of these keywords in the campaign names. The script extracts these keywords and assigns them to a new column in the dataset. It then identifies which campaigns have been categorized and prepares a subset of the data containing only these categorized campaigns. If no campaigns are categorized, it prepares an empty dataset. This process helps in organizing and analyzing marketing campaigns based on their strategic focus.
Walking Through the Code
- Define Constants and Patterns
- The script begins by defining constants for column names used in the dataset, such as
RPT_COL_CAMPAIGN
,RPT_COL_ACCOUNT
, andRPT_COL_BRAND_LOCAL_EMERGENCY
. - A regular expression pattern
PATTERN1
is defined to match the keywords “Brand,” “Local,” or “Emergency” in the campaign names.
- The script begins by defining constants for column names used in the dataset, such as
- Extract and Assign Dimension Values
- The script uses the
str.extract()
method to find occurrences of the defined pattern in theCampaign
column and assigns these values to theBrand | Local | Emergency
column. If no match is found, it fills the column with a space.
- The script uses the
- Identify and Select Changed Campaigns
- It checks for changes in the
Brand | Local | Emergency
column to identify campaigns that have been categorized. - The script selects only the relevant columns (
Campaign
,Account
, andBrand | Local | Emergency
) for further processing.
- It checks for changes in the
- Prepare Output DataFrame
- If there are any categorized campaigns, they are stored in
outputDf
, and the data is printed. - If no campaigns are categorized, an empty
outputDf
is prepared.
- If there are any categorized campaigns, they are stored in
Vitals
- Script ID : 535
- Client ID / Customer ID: 1306917127 / 60268084
- Action Type: Bulk Upload
- Item Changed: Campaign
-
Output Columns: Account, Campaign, ___Brand Local Emergency___ - Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: ascott@marinsoftware.com (ascott@marinsoftware.com)
- Created by ascott@marinsoftware.com on 2023-11-17 14:53
- Last Updated by ascott@marinsoftware.com on 2023-12-06 04:01
> 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
#
# Assign Campaign Dimension Values according to:
# - Brand | Local | Emergency
#
# Author: Jeremy Brown & Adam Scott
# Date: 2023-11-17
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_BRAND_LOCAL_EMERGENCY = 'Brand | Local | Emergency'
# define pattern match
PATTERN1 = r'(Brand|Local|Emergency)'
# find all occurrences of the pattern in the Campaign name
inputDf[RPT_COL_BRAND_LOCAL_EMERGENCY] = inputDf[RPT_COL_CAMPAIGN].str.extract(PATTERN1).fillna(' ')
# find changed campaigns
changed = inputDf[RPT_COL_BRAND_LOCAL_EMERGENCY].ne(' ') | inputDf[RPT_COL_BRAND_LOCAL_EMERGENCY].ne(' ')
#selecting only relevant cols
inputDf= inputDf[[RPT_COL_CAMPAIGN,RPT_COL_ACCOUNT,RPT_COL_BRAND_LOCAL_EMERGENCY]]
# put changed campaigns into outputDf; if none, prepare empty outputDf
if sum(changed) > 0:
#print('changed data',tableize(inputDf[changed]))
outputDf = inputDf[changed]
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
outputDf = outputDf.iloc[0:0]##
Post generated on 2024-11-27 06:58:46 GMT