Script 905: Campaign naming 1st separator tag example
Purpose
Python script to add a tag to a campaign name based on a specified separator.
To Elaborate
This Python script solves the problem of adding a tag to a campaign name based on a specified separator. The script takes an input DataFrame with campaign names and existing tags, and adds a new tag to the campaign name if it meets certain conditions. The new tag is extracted from the campaign name using a specified separator. The script then outputs the modified DataFrame with the added tags.
Walking Through the Code
- The script starts by defining configurable parameters, including the separator and the location of the tag within the campaign name.
- Next, the script defines constants for the column names in the input and output DataFrames.
- The script defines a function called
get_tag_from_campaign_name
that takes a campaign name as input and extracts the tag using the specified separator. - The script creates a copy of the input DataFrame as the output DataFrame.
- The script iterates through each row in the input DataFrame.
- For each row, the script retrieves the existing tag and campaign name.
- The script calls the
get_tag_from_campaign_name
function to extract the tag from the campaign name. - The script prints the campaign name and extracted tag.
- The script checks if the extracted tag is different from the existing tag and if it is not empty.
- If the conditions are met, the script updates the output DataFrame with the extracted tag.
- If the conditions are not met, the script updates the output DataFrame with a NaN value.
- After iterating through all rows, the script filters the output DataFrame to include only rows with non-empty tags.
- If the output DataFrame is not empty, the script prints the tableized output DataFrame.
- If the output DataFrame is empty, the script prints “Empty outputDf”.
Vitals
- Script ID : 905
- Client ID / Customer ID: 1306927585 / 60270327
- Action Type: Bulk Upload (Preview)
- Item Changed: Campaign
- Output Columns: Account, Campaign, PostSeparatorTag
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Anton Antonov (aantonov@marinsoftware.com)
- Created by Anton Antonov on 2024-04-10 15:38
- Last Updated by Anton Antonov on 2024-04-10 15:56
> 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
#
# Add Dimensions Tag based on Campaign Name
#
#
# Author: Michael S. Huang
# Date: 2023-05-17
#
########### Configurable Params - START ##########
SEP = ':'
TAG_LOCATION = 1 # comes after first separator
########### Configurable Params - END ###########
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_POSTSEPARATORTAG = 'PostSeparatorTag'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_POSTSEPARATORTAG = 'PostSeparatorTag'
# define function to parse tag from campaign name
def get_tag_from_campaign_name(campaign_name):
vals = campaign_name.split(SEP)
# assume tag value is after 1st separator
tag = ''
try:
tag = str(vals[TAG_LOCATION])
except:
tag = ''
return tag
# copy all input rows to output
outputDf = inputDf.copy()
# loop through all rows
for index, row in inputDf.iterrows():
existing_tag = row[RPT_COL_POSTSEPARATORTAG]
campaign_name = row[RPT_COL_CAMPAIGN]
tag = get_tag_from_campaign_name(campaign_name)
print("Campaign [%s] => Tag [%s]" % (campaign_name, tag))
# only tag if it's different than existing tag
if (len(tag) > 0) & (tag != existing_tag):
outputDf.at[index, BULK_COL_POSTSEPARATORTAG] = tag
else:
outputDf.at[index, BULK_COL_POSTSEPARATORTAG] = np.nan
# only include non-empty tags in bulk
outputDf = outputDf.loc[ outputDf[BULK_COL_POSTSEPARATORTAG].notnull() ]
if not outputDf.empty:
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
Post generated on 2024-05-15 07:44:05 GMT