Script 997: NonBrand Campaign Categories Auto Dim Tag
Purpose:
The script extracts and categorizes tags from non-brand campaign names in a dataset.
To Elaborate
The Python script is designed to process a dataset containing information about marketing campaigns, specifically focusing on non-brand campaigns. It extracts specific tags from the campaign names based on a predefined separator and stores these tags in a designated column. The script is particularly useful for categorizing and organizing non-brand campaigns by their tags, which can help in analyzing and managing marketing strategies more effectively. The script ensures that only campaigns with valid tags are included in the final output, thus maintaining data relevance and accuracy.
Walking Through the Code
- Input and Configuration:
- The script begins by loading the primary data source into
inputDf
. - Configurable parameters include
SEP
, which defines the separator used in campaign names, andTAG_LOCATION
, which specifies the position of the tag within the split campaign name.
- The script begins by loading the primary data source into
- Function Definition:
- A function
extract_tag
is defined to split the campaign name using the separator and extract the tag located at the specified position. If the tag position is not available, it returns an empty string.
- A function
- Data Processing:
- The script creates a copy of the input dataframe to
outputDf
. - It iterates over each row in the input dataframe, extracting the campaign name and checking for the presence of the separator.
- If the separator is found, the script extracts the tag using the
extract_tag
function and stores it in theCategories of NB Campaigns
column ofoutputDf
.
- The script creates a copy of the input dataframe to
- Output Filtering:
- The script filters out rows with empty tags to ensure only relevant data is included in the final output.
- It prints the resulting dataframe if it contains any data, otherwise, it indicates that the output is empty.
Vitals
- Script ID : 997
- Client ID / Customer ID: 1306927757 / 60270153
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Categories of NB Campaigns
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
- Created by dwaidhas@marinsoftware.com on 2024-04-24 17:19
- Last Updated by dwaidhas@marinsoftware.com on 2024-04-24 17:42
> 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
60
61
62
63
64
65
66
##
## name: NonBrand Campaign Categories Auto Dim Tag
## description:
##
##
## author: Dana Waidhas
## created: 2024-04-24
##
inputDf = dataSourceDict["1"]
# Configurable Params
SEP = '|'
TAG_LOCATION = 1 # Comes after the third separator
# primary data source and columns
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_BRAND_VS_NONBRAND = 'Brand vs NonBrand'
RPT_COL_CATEGORIES_OF_NB_CAMPAIGNS = 'Categories of NB Campaigns'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_CATEGORIES_OF_NB_CAMPAIGNS = 'Categories of NB Campaigns'
outputDf[BULK_COL_CATEGORIES_OF_NB_CAMPAIGNS] = "<<YOUR VALUE>>"
# user code start here
# Function to extract tag from campaign
def extract_tag(campaign):
parts = campaign.split(SEP)
if len(parts) > TAG_LOCATION:
return parts[TAG_LOCATION].strip()
else:
return ""
# Apply the function to extract tags from campaigns and store in "Categories of NB Campaigns" column
outputDf = inputDf.copy()
# Loop through all rows
for index, row in inputDf.iterrows():
campaign_name = row[RPT_COL_CAMPAIGN]
# Skip processing if campaign name does not contain the separator
if SEP not in campaign_name:
continue
tag = extract_tag(campaign_name)
# Print campaign and tag information
print("Campaign [%s] => Tag [%s]" % (campaign_name, tag))
# Store the tag in the output dataframe
outputDf.at[index, BULK_COL_CATEGORIES_OF_NB_CAMPAIGNS] = tag
# Only include non-empty tags in bulk
outputDf = outputDf.dropna(subset=[BULK_COL_CATEGORIES_OF_NB_CAMPAIGNS])
# Print the output dataframe
if not outputDf.empty:
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
Post generated on 2025-03-11 01:25:51 GMT