Script 1027: Tagging Social dimensions targeting and platform

Purpose:

The Python script processes campaign data to extract and tag platform and targeting information from campaign names.

To Elaborate

The Python script is designed to process a dataset containing campaign information, specifically focusing on extracting and tagging platform and targeting details from the campaign names. The script assumes that these details are embedded within the campaign names, separated by a specific character (a hyphen in this case). By parsing the campaign names, the script identifies and assigns the platform and targeting information to the appropriate columns in the dataset. This process helps in organizing and structuring the data for further analysis or reporting, ensuring that each campaign is accurately tagged with its respective platform and targeting attributes.

Walking Through the Code

  1. Configurable Parameters:
    • The script begins by defining several constants, including the separator used in campaign names (SEP) and column names for both the report and bulk data. These parameters can be adjusted by the user to fit different data structures or naming conventions.
  2. Function Definition:
    • A function extract_tags is defined to split the campaign name using the specified separator. It extracts the targeting and platform information based on their positions in the split segments. This function returns the extracted targeting and platform values.
  3. Data Preparation:
    • The script creates a copy of the input DataFrame (inputDf) to outputDf, which will be used to store the processed data.
  4. Data Processing Loop:
    • The script iterates over each row in the input DataFrame. For each campaign name, it calls the extract_tags function to obtain the targeting and platform information.
    • It then updates the corresponding columns in the outputDf with these extracted values.
  5. Data Cleaning:
    • After processing all rows, the script removes any rows in outputDf that have missing values in the targeting or platform columns to ensure data integrity.
  6. Output Check:
    • Finally, the script checks if outputDf is empty and prints the processed data if available, or indicates that the output is empty.

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 2025-03-11 01:25:51 GMT

comments powered by Disqus