Script 1327: Set KW Template
Purpose
The Python script processes keyword data to generate a structured keyword template based on specific group terms.
To Elaborate
The script is designed to process a dataset containing keyword information and generate a structured keyword template. It focuses on transforming keywords based on their associated group names. The primary task is to check if certain predefined terms are present in the group names. If these terms are found, the keyword is used as-is in lowercase for the template. If not, the group name within the keyword is replaced with a placeholder “[LOCATION]”. This transformation helps in standardizing keyword templates for further analysis or use in marketing campaigns. The script ensures that the original data remains unchanged by working on a copy and outputs a DataFrame with the necessary columns for further use.
Walking Through the Code
- Data Preparation
- The script begins by creating a copy of the input DataFrame to prevent modifications to the original data.
- It converts the ‘Keyword’ and ‘Group’ columns to lowercase and removes any plus signs from ‘Keyword’ to standardize the data.
- Initialization
- Two new columns, ‘KW Template’ and ‘KWCHECK’, are initialized with default values. ‘KW Template’ is set to a placeholder value, and ‘KWCHECK’ is set to “YES”.
- Processing Logic
- A list of special terms is defined to identify specific group names that require special handling.
- The script iterates over each row of the DataFrame, checking if any special term is present in the ‘Group’ column.
- If a special term is found, the ‘KW Template’ is set to the lowercase version of the ‘Keyword’.
- If no special term is found, the script replaces the group name in the ‘Keyword’ with “[LOCATION]” in the ‘KW Template’.
- Output Preparation
- The script selects the necessary columns for the output DataFrame, ensuring only relevant information is included.
- The processed DataFrame is returned and assigned to the output variable for further use.
Vitals
- Script ID : 1327
- Client ID / Customer ID: 1306927569 / 60270325
- Action Type: Bulk Upload
- Item Changed: Keyword
- Output Columns: Account, Campaign, Group, Keyword, Match Type, KW Template, KWCHECK
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Jeremy Brown (jbrown@marinsoftware.com)
- Created by Jeremy Brown on 2024-08-15 10:23
- Last Updated by Jeremy Brown on 2024-08-15 10:31
> 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
MATCH_TYPE = {
'EXACT': 'exact',
'PHRASE': 'phrase',
'BROAD': 'broad',
}
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
# primary data source and columns
inputDf = dataSourceDict["1"]
RPT_COL_KEYWORD = 'Keyword'
RPT_COL_MATCH_TYPE = 'Match Type'
RPT_COL_GROUP = 'Group'
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_STATUS = 'Status'
RPT_COL_KW_TEMPLATE = 'KW Template'
RPT_COL_KWCHECK = 'KWCHECK'
RPT_COL_IMPR = 'Impr.'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_GROUP = 'Group'
BULK_COL_KEYWORD = 'Keyword'
BULK_COL_MATCH_TYPE = 'Match Type'
BULK_COL_KW_TEMPLATE = 'KW Template'
BULK_COL_KWCHECK = 'KWCHECK'
outputDf[BULK_COL_KW_TEMPLATE] = "<<YOUR VALUE>>"
outputDf[BULK_COL_KWCHECK] = "<<YOUR VALUE>>"
# user code start here
def process(inputDf):
# Create a copy of the DataFrame to avoid modifying the original data
df = inputDf.copy()
# Convert 'Keyword' and 'Group' to lowercase and remove any pluses from 'Keyword'
df['Keyword_lower'] = df['Keyword'].str.lower().str.replace('+', '', regex=False)
df['Group_lower'] = df['Group'].str.lower()
# Initialize 'KW Template' and 'KWCHECK' columns with default values
df['KW Template'] = "<<YOUR VALUE>>"
df['KWCHECK'] = "YES"
# Define the specific terms that will be checked in the 'Group' column
special_terms = ["local", "lock", "- lock", "general", "gen -"]
# Iterate over each row in the DataFrame
for i, row in df.iterrows():
group_value = row['Group_lower']
keyword_value = row['Keyword_lower']
# Check if any special term is in the 'Group' value
if any(term in group_value for term in special_terms):
# If a special term is found, set 'KW Template' to the lowercase 'Keyword'
df.at[i, 'KW Template'] = keyword_value
else:
# Otherwise, replace the 'Group' string in 'Keyword' with "[LOCATION]"
df.at[i, 'KW Template'] = keyword_value.replace(group_value, "[LOCATION]")
# Select only the required columns for the output DataFrame
outputDf = df[['Keyword', 'Match Type', 'Group', 'Campaign', 'Account', 'KW Template', 'KWCHECK']]
# Print the DataFrame for debug purposes
print(outputDf)
return outputDf
# Assign the processed DataFrame to outputDf
outputDf = process(inputDf)
Post generated on 2024-11-27 06:58:46 GMT