Script 213: Brand vs Non Brand Dimension
Purpose
Python script for tagging campaigns as “Brand” or “Non Brand” based on the campaign name.
To Elaborate
The Python script solves the problem of categorizing campaigns as “Brand” or “Non Brand” based on the campaign name. It takes an input DataFrame and checks if the campaign name contains the word “Brand”. It then creates two separate DataFrames, one for Brand campaigns and one for Non Brand campaigns. Finally, it merges the two DataFrames and outputs the result.
Walking Through the Code
- The script defines column constants for the campaign and account columns.
- It checks if the campaign name contains the word “Brand” using the
str.contains()
method. - Based on the check, it creates two separate DataFrames:
brandDf
for Brand campaigns andnonbrandDf
for Non Brand campaigns. - It updates the
BULK_COL_B_NB
column in both DataFrames to “Brand” and “Non Brand” respectively. - It merges the two DataFrames using the
merge()
method with thehow='outer'
parameter. - The resulting merged DataFrame is assigned to
outputDf
. - The script prints the tableized version of the
outputDf
DataFrame using thetableize()
function.
Vitals
- Script ID : 213
- Client ID / Customer ID: 1306925889 / 60269721
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Brand vs Non Brand
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: ascott@marinsoftware.com (ascott@marinsoftware.com)
- Created by ascott@marinsoftware.com on 2023-06-20 19:22
- Last Updated by ascott@marinsoftware.com on 2023-12-06 04:01
> 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
#
# Bran/Non-Brand Dimension Tagging
#
# Byron Porter
# 2023-05-25
#
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_B_NB = 'Brand vs Non Brand'
# Check if RPT_COL_CAMPAIGN column has 'Brand' in the campaign name
bcheck = (inputDf[RPT_COL_CAMPAIGN].str.contains('Brand', case=True, na=False))
# Use check to create two DataFrames. One for Brand and one for Non-Brand tagging
brandDf = inputDf.loc[bcheck, [BULK_COL_ACCOUNT, BULK_COL_CAMPAIGN, BULK_COL_B_NB]]
brandDf.loc[:, BULK_COL_B_NB] = 'Brand'
nonbrandDf = inputDf.loc[~bcheck, [BULK_COL_ACCOUNT, BULK_COL_CAMPAIGN, BULK_COL_B_NB]]
nonbrandDf.loc[:, BULK_COL_B_NB] = 'Non Brand'
# Merges the Brand and Non-Brand dataframes
outputDf = brandDf.merge(nonbrandDf, how='outer')
# Print the tableized version of the output DataFrame
print(tableize(outputDf))
Post generated on 2024-03-10 06:34:12 GMT