Script 1021: Tagging dimension Script Campaigns Keyword Match Type

Purpose

Python script to extract keyword match type from campaign names and update the keyword match type column in the output dataframe.

To Elaborate

The Python script solves the problem of extracting the keyword match type from campaign names and updating the keyword match type column in the output dataframe. The script uses a list of intent keywords and a separator to identify the keyword match type in the campaign name. It then updates the keyword match type column in the output dataframe with the extracted match type. Rows with missing keyword match type values are dropped from the output dataframe.

Walking Through the Code

  1. The script defines the configurable parameters, including the intent keywords and the separator used after the intent keywords.
  2. The script defines a function, extract_keyword_match_type, to extract the keyword match type from a campaign name.
  3. The script creates a copy of the input dataframe as the output dataframe.
  4. The script loops through all rows in the input dataframe.
  5. For each row, it extracts the campaign name and calls the extract_keyword_match_type function to extract the keyword match type.
  6. It prints the campaign name and the extracted keyword match type.
  7. It updates the keyword match type column in the output dataframe with the extracted match type.
  8. It drops any rows with missing keyword match type values from the output dataframe.
  9. If the output dataframe is not empty, it prints the tableized output dataframe. Otherwise, it prints “Empty outputDf”.

Vitals

  • Script ID : 1021
  • Client ID / Customer ID: 1306927457 / 60270313
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Keyword Match Type
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Autumn Archibald (aarchibald@marinsoftware.com)
  • Created by Autumn Archibald on 2024-04-29 19:44
  • Last Updated by Autumn Archibald on 2024-04-29 19:51
> 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
# Define the configurable parameters for the script
INTENT_KEYWORDS = ['tofu', 'mofu', 'bofu']
SEP = '-'  # Separator used after intent keywords
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_KEYWORD_MATCH_TYPE = 'Keyword Match Type'

# Function to extract keyword match type from campaign name
def extract_keyword_match_type(campaign_name):
    for intent in INTENT_KEYWORDS:
        separator = intent + SEP
        if separator in campaign_name:
            start_index = campaign_name.find(separator) + len(separator)
            end_index = campaign_name.find(SEP, start_index)
            if end_index != -1:
                return campaign_name[start_index:end_index]
            else:
                return campaign_name[start_index:]
    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 keyword match type from campaign name
    keyword_match_type = extract_keyword_match_type(campaign_name)
    print("Campaign [%s] => Keyword Match Type [%s]" % (campaign_name, keyword_match_type))

    # Update keyword match type column
    outputDf.at[index, BULK_COL_KEYWORD_MATCH_TYPE] = keyword_match_type

# Drop any rows with missing keyword match type values
outputDf = outputDf.dropna(subset=[BULK_COL_KEYWORD_MATCH_TYPE])

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