Script 147: Dimension AutoApply Brand NonBrand

Purpose

The script automates the process of tagging campaigns as either “Brand” or “Non-Brand” based on the presence of the word “Brand” in the campaign name.

To Elaborate

The Python script is designed to automate the categorization of marketing campaigns into “Brand” and “Non-Brand” categories. This is achieved by examining the campaign names within a dataset to determine if they contain the word “Brand”. If the word is present, the campaign is tagged as “Brand”; otherwise, it is tagged as “Non-Brand”. This categorization helps in organizing and analyzing marketing data more effectively, allowing for better budget allocation and performance tracking. The script processes the data by creating separate dataframes for each category and then merges them to produce a comprehensive output that reflects the structured budget allocation (SBA) for brand-related and non-brand-related campaigns.

Walking Through the Code

  1. Initial Setup
    • The script begins by defining constants for column names used in the input and output dataframes. These constants help in maintaining consistency and readability throughout the script.
  2. Brand Check
    • A boolean check (bcheck) is performed on the RPT_COL_CAMPAIGN column of the input dataframe to identify rows where the campaign name contains the word “Brand”. This check is case-sensitive and excludes any missing values.
  3. DataFrame Creation
    • Two separate dataframes are created:
      • brandDf for campaigns identified as “Brand”, where the BULK_COL_B_NB column is set to “Brand”.
      • nonbrandDf for campaigns not identified as “Brand”, where the BULK_COL_B_NB column is set to “Non-Brand”.
  4. Merging DataFrames
    • The brandDf and nonbrandDf dataframes are merged using an outer join to ensure all campaigns are included in the final output, regardless of their categorization.
  5. Output
    • The resulting merged dataframe (outputDf) is printed in a table format, displaying the account, campaign, and their respective “Brand vs Non-Brand” tags.

Vitals

  • Script ID : 147
  • Client ID / Customer ID: 200958251 / 5139
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Brand vs NonBrand
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Jonathan Reichl (jreichl@marinsoftware.com)
  • Created by Jonathan Reichl on 2023-05-30 09:51
  • Last Updated by Grégory Pantaine 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
#
# Automating "Brand vs NonBrand" Dimension Tagging
# Taken from Byron Porter's Fisher Investment account
# setup 2023-11-15 by Greg Pantaine
#

RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_B_NB = 'Brand vs NonBrand'

# 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(outputDf.to_string(index=False))

Post generated on 2024-11-27 06:58:46 GMT

comments powered by Disqus