Script 691: Campaign Name Dimension Auto Tagging School Program
Purpose
Python script that combines the ‘School’ and ‘Program’ columns into a new column called ‘School_Program’ and assigns the created tag to the ‘School_Program’ column.
To Elaborate
The Python script solves the problem of combining the ‘School’ and ‘Program’ columns in a dataset into a single column called ‘School_Program’. This is useful for tagging purposes, where the combination of these two dimensions provides more specific information about a campaign. The script also checks if either the ‘School’ or ‘Program’ column is empty and skips processing for those rows. The final output is a dataframe with the ‘School_Program’ column populated with the created tags.
Walking Through the Code
- The script starts by defining the primary data source and the column names for the input dataframe.
- It also defines the output column names and their initial values.
- The script then loops through each row in the input dataframe.
- For each row, it retrieves the values of the ‘School’ and ‘Program’ columns.
- If either the ‘School’ or ‘Program’ value is empty, the script skips processing for that row.
- If both values are present, the script creates a tag by concatenating the ‘School’ and ‘Program’ values with an underscore.
- The created tag is printed for information.
- The created tag is assigned to the ‘School_Program’ column in the output dataframe.
- After processing all rows, the script removes rows from the output dataframe where both the ‘School_Program’ and ‘SBA Strategy’ columns are blank.
- If the output dataframe is not empty, an example of the dataframe is printed.
- If the output dataframe is empty, a message indicating that it is empty is printed.
Vitals
- Script ID : 691
- Client ID / Customer ID: 1306926629 / 60270083
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, School_Program, SBA Strategy
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
- Created by dwaidhas@marinsoftware.com on 2024-02-09 19:49
- Last Updated by Michael Huang on 2024-03-19 04:11
> 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
##
## name: Campaign Name Dimension Auto Tagging: School_Program
## description:
## Combine Dimensions columns 'School' and 'Program' into column 'School_Program'
##
## author: Dana Waidhas
## created: 2024-02-09
##
# Primary data source and columns
inputDf = dataSourceDict["1"]
# Output columns and initial values
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_SCHOOL = 'School'
RPT_COL_PROGRAM = 'Program'
RPT_COL_SCHOOL_PROGRAM = 'School_Program'
RPT_COL_SBA_STRATEGY = 'SBA Strategy'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_SCHOOL = 'School'
BULK_COL_PROGRAM = 'Program'
BULK_COL_SCHOOL_PROGRAM = 'School_Program'
BULK_COL_SBA_STRATEGY = 'SBA Strategy'
# Loop through all rows
for index, row in inputDf.iterrows():
school = row[RPT_COL_SCHOOL]
program = row[RPT_COL_PROGRAM]
# Skip processing if either School or Program is empty
if pd.isna(school) or pd.isna(program):
continue
# Create School_Program tag by concatenating School and Program with an underscore
tag = f"{school}_{program}"
# Print tag information
print("Tag [%s]" % tag)
# Assign the created tag to BULK_COL_SCHOOL_PROGRAM
outputDf.at[index, BULK_COL_SCHOOL_PROGRAM] = tag
# Don't include in Bulk if both tags are blank
outputDf = outputDf.dropna(subset=[BULK_COL_SCHOOL_PROGRAM, BULK_COL_SBA_STRATEGY], how='all')
if not outputDf.empty:
print("outputDf example", tableize(outputDf.head()))
else:
print("Empty outputDf")
Post generated on 2024-05-15 07:44:05 GMT