Script 997: NonBrand Campaign Categories Auto Dim Tag
Purpose
Python script to extract tags from campaign names and store them in the “Categories of NB Campaigns” column.
To Elaborate
The Python script solves the problem of extracting tags from campaign names and storing them in a specific column. It takes a dataframe as input, loops through each row, extracts the tag from the campaign name, and stores it in the “Categories of NB Campaigns” column. The script also handles cases where the campaign name does not contain the separator.
Walking Through the Code
- The script starts by defining the input dataframe and configurable parameters.
- It also defines the columns used in the input and output dataframes.
- The initial value of the “Categories of NB Campaigns” column in the output dataframe is set to “«YOUR VALUE»”.
- The script defines a function called “extract_tag” that takes a campaign name as input and extracts the tag using the separator.
- The script creates a copy of the input dataframe and assigns it to the output dataframe.
- It then loops through each row in the input dataframe.
- For each row, it checks if the campaign name contains the separator. If not, it skips processing for that row.
- If the campaign name contains the separator, it calls the “extract_tag” function to extract the tag.
- The campaign name and tag information are printed.
- The tag is stored in the “Categories of NB Campaigns” column of the output dataframe.
- After looping through all rows, the script drops rows with empty tags from the output dataframe.
- Finally, the output dataframe is printed if it is not empty. Otherwise, a message indicating an empty dataframe is printed.
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 2024-05-15 07:44:05 GMT