Script 1027: Tagging Social dimensions targeting and platform

Purpose

Tagging Social dimensions targeting and platform

To Elaborate

This Python script extracts the targeting and platform information from campaign names and updates the corresponding columns in the output dataframe. It also drops any rows with missing values.

Walking Through the Code

  1. The configurable parameters for the script are defined, including the separator used in campaign names and the column names for the input and output dataframes.
  2. The extract_tags function is defined to split the campaign name and extract the targeting and platform information.
  3. The input dataframe is copied to the output dataframe.
  4. A loop iterates through all the rows in the input dataframe.
  5. For each row, the campaign name is extracted.
  6. The extract_tags function is called to get the targeting and platform from the campaign name.
  7. The targeting and platform values are printed for each campaign.
  8. The targeting and platform columns in the output dataframe are updated with the extracted values.
  9. Any rows with missing values in the targeting or platform columns are dropped from the output dataframe.
  10. If the output dataframe is not empty, it is printed in a tabular format.
  11. If the output dataframe is empty, a message indicating that is printed.

Vitals

  • Script ID : 1027
  • Client ID / Customer ID: 1306927457 / 60270313
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Targeting, Platform
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Autumn Archibald (aarchibald@marinsoftware.com)
  • Created by Autumn Archibald on 2024-04-29 23:01
  • Last Updated by Autumn Archibald on 2024-04-30 04:54
> 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
# Define the configurable parameters for the script
SEP = '-'  # Separator used in campaign names
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_PLATFORM = 'Platform'
BULK_COL_TARGETING = 'Targeting'

# Function to extract platform and targeting from campaign name
def extract_tags(campaign_name):
    segments = campaign_name.split(SEP)
    targeting = segments[-2] if len(segments) >= 2 else ''
    platform = segments[-1] if len(segments) >= 1 else ''
    return targeting, platform

# 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 platform and targeting from campaign name
    targeting, platform = extract_tags(campaign_name)
    print("Campaign [%s] => Targeting [%s], Platform [%s]" % (campaign_name, targeting, platform))

    # Update columns
    outputDf.at[index, BULK_COL_TARGETING] = targeting
    outputDf.at[index, BULK_COL_PLATFORM] = platform

# Drop any rows with missing values
outputDf = outputDf.dropna(subset=[BULK_COL_TARGETING, BULK_COL_PLATFORM])

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