Script 1017: Tagging dimension Script Campaigns Topic

Purpose

Tagging dimension Script - Campaigns - Topic

To Elaborate

This Python script solves the problem of extracting the topic from campaign names in a dataset. It takes a dataset with campaign names as input and outputs a dataset with a new column called “Topic” that contains the extracted topics from the campaign names.

Walking Through the Code

  1. The script starts by defining configurable parameters such as the separator used in campaign names and the locations of the tag and topic in the campaign name.
  2. It then defines a function called “extract_topic” that takes a campaign name as input and extracts the topic from it using the defined separator.
  3. The script copies the input dataset to an output dataset.
  4. It loops through all the rows in the input dataset.
  5. For each row, it extracts the campaign name and calls the “extract_topic” function to get the topic.
  6. It prints the campaign name and the extracted topic.
  7. It updates the “Topic” column in the output dataset with the extracted topic.
  8. It drops any rows in the output dataset that have missing values in the “Topic” column.
  9. If the output dataset is not empty, it prints the tableized version of the output dataset. Otherwise, it prints “Empty outputDf”.

Vitals

  • Script ID : 1017
  • Client ID / Customer ID: 1306927457 / 60270313
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Topic
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Autumn Archibald (aarchibald@marinsoftware.com)
  • Created by Autumn Archibald on 2024-04-29 19:12
  • Last Updated by Autumn Archibald on 2024-04-29 19:13
> 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
# Define the configurable parameters for the script
SEP = '_'  # Separator used in campaign names
TAG_LOCATION = 0  # First word before the first separator
TOPIC_LOCATION = 1  # Second word before the second separator
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_TOPIC = 'Topic'

# Function to extract Topic from campaign name
def extract_topic(campaign_name):
    if SEP in campaign_name:
        segments = campaign_name.split(SEP)
        topic = segments[TOPIC_LOCATION] if len(segments) > TOPIC_LOCATION else ''
        return topic
    else:
        return ''

# Copy input rows to output
outputDf = inputDf.copy()

# Loop through all rows
for index, row in inputDf.iterrows():
    campaign_name = row[RPT_COL_CAMPAIGN]

    # Extract Topic from campaign name
    topic = extract_topic(campaign_name)
    print("Campaign [%s] => Topic [%s]" % (campaign_name, topic))

    # Update Topic column
    outputDf.at[index, BULK_COL_TOPIC] = topic

# Drop any rows with missing Topic values
outputDf = outputDf.dropna(subset=[BULK_COL_TOPIC])

if not outputDf.empty:
    print("outputDf", tableize(outputDf))
else:
    print("Empty outputDf")

Post generated on 2024-05-15 07:44:05 GMT

comments powered by Disqus