Script 835: Auto Tag Campaign with Tipo de Campanha Dimension
Purpose
Auto Tag Campaign with Tipo de Campanha Dimension
To Elaborate
The Python script solves the problem of automatically tagging campaigns based on specific keywords in their names. It extracts the tag (Tipo de campanha) from the campaign name and updates the output dataframe with the extracted tag.
Walking Through the Code
- The script starts by importing the necessary libraries and defining the input dataframe.
- It defines the column constants for the primary data source and the output columns.
- The script includes a function called
extract_tag_from_campaign_name
that takes a campaign name as input and returns the corresponding tag based on specific keywords. - The script creates an output dataframe by copying the input dataframe.
- It iterates through each row in the input dataframe.
- For each row, it extracts the campaign name and calls the
extract_tag_from_campaign_name
function to get the tag. - It prints the campaign name and tag information.
- The script updates the output dataframe with the extracted tag.
- It drops rows with NaN values in the ‘Tipo de campanha’ column.
- Finally, it displays the resulting dataframe.
Vitals
- Script ID : 835
- Client ID / Customer ID: 1306927165 / 60270223
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Tipo de campanha
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
- Created by dwaidhas@marinsoftware.com on 2024-03-22 16:52
- Last Updated by dwaidhas@marinsoftware.com on 2024-03-22 19:00
> 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
##
## name: Auto Tag Campaign with Tipo de Campanha Dimension
## description:
##
##
## author: Dana Waidhas
## created: 2024-03-22
##
inputDf = dataSourceDict["1"]
# primary data source and columns
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_TIPO_DE_CAMPANHA = 'Tipo de campanha'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_TIPO_DE_CAMPANHA = 'Tipo de campanha'
# user code start here
# Function to extract tag from campaign name
def extract_tag_from_campaign_name(campaign_name):
"""
Extracts the tag (Tipo de campanha) from the campaign name based on specific keywords.
Args:
campaign_name (str): The name of the campaign.
Returns:
str or None: The extracted tag (Tipo de campanha) if found, else None.
"""
# Convert the campaign name to lowercase for case-insensitive matching
campaign_name_lower = campaign_name.lower()
# Check for keywords and return the corresponding tag
if 'curso' in campaign_name_lower:
return 'Curso'
elif 'generica' in campaign_name_lower:
return 'Generica'
elif 'oportunidade' in campaign_name_lower:
return 'Oportunidade'
elif 'competitor' in campaign_name_lower:
return 'Competitor'
else:
return None
# Copy all input rows to output
outputDf = inputDf.copy()
# Iterate through all rows
for index, row in inputDf.iterrows():
campaign_name = row[RPT_COL_CAMPAIGN]
# Extract tag from campaign name
tag = extract_tag_from_campaign_name(campaign_name)
# Print campaign and tag information
print("Campaign [%s] => Tag [%s]" % (campaign_name, tag))
# Update the output dataframe with the extracted tag
outputDf.at[index, BULK_COL_TIPO_DE_CAMPANHA] = tag
# Drop rows with NaN values in the 'Tipo de campanha' column
outputDf = outputDf.dropna(subset=[BULK_COL_TIPO_DE_CAMPANHA])
# Display the resulting dataframe
if not outputDf.empty:
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
Post generated on 2024-05-15 07:44:05 GMT