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 platform and targeting details embedded within campaign names. The script assumes that these details are encoded in a structured format within the campaign name, separated by a defined character. By parsing the campaign names, the script identifies and tags the relevant platform and targeting information, updating the dataset accordingly. This process helps in organizing and structuring the campaign data, making it easier to analyze and utilize for marketing strategies. The script also ensures data integrity by removing any rows with missing platform or targeting information.
Walking Through the Code
- Configurable Parameters:
- The script begins by defining several constants, including the separator used in campaign names (
SEP
) and column names for both input and output dataframes. These parameters can be adjusted by the user to match the specific format and structure of their data.
- The script begins by defining several constants, including the separator used in campaign names (
- Function Definition:
- A function
extract_tags
is defined to split the campaign name using the specified separator. It extracts the second-to-last segment as the targeting information and the last segment as the platform information.
- A function
- Data Processing:
- The script creates a copy of the input dataframe to preserve the original data. It then iterates over each row, extracting the campaign name and using the
extract_tags
function to obtain the targeting and platform tags.
- The script creates a copy of the input dataframe to preserve the original data. It then iterates over each row, extracting the campaign name and using the
- Data Update:
- For each row, the script updates the corresponding columns in the output dataframe with the extracted targeting and platform information.
- Data Cleaning:
- The script removes any rows from the output dataframe that have missing values in the targeting or platform columns to ensure the dataset’s completeness and accuracy.
- Output Handling:
- Finally, the script checks if the output dataframe is empty and prints the results accordingly, providing a structured view of the processed data.
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-11-27 06:58:46 GMT