Script 133: Dimension Update

Purpose

The Python script categorizes campaigns into “Brand” or “Non-Brand” based on the presence of the word “Brand” in their names.

To Elaborate

The Python script is designed to classify marketing campaigns into two categories: “Brand” and “Non-Brand.” This classification is based on whether the campaign name contains the word “Brand.” The script processes a DataFrame containing campaign data, checks each campaign name for the presence of the word “Brand,” and then tags the campaign accordingly. The result is two separate DataFrames, one for “Brand” campaigns and another for “Non-Brand” campaigns, which are then merged into a single output DataFrame. This categorization helps in organizing and analyzing marketing efforts by distinguishing between campaigns that are directly associated with the brand and those that are not.

Walking Through the Code

  1. Initial Setup
    • The script defines constants for column names used in the DataFrame, such as ‘Campaign’ and ‘Account’.
    • It checks if the ‘Campaign’ column contains the word “Brand” using a case-insensitive search.
  2. DataFrame Creation
    • Two DataFrames are created: brandDf for campaigns containing “Brand” and nonbrandDf for those that do not.
    • The ‘Brand/Non-Brand’ column is updated to reflect the categorization.
  3. Merging and Output
    • The script merges the brandDf and nonbrandDf DataFrames into a single outputDf.
    • The final DataFrame is printed in a table format for easy viewing.

Vitals

  • Script ID : 133
  • Client ID / Customer ID: 1306922587 / 2
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Brand/Non-Brand
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Byron Porter (bporter@marinsoftware.com)
  • Created by Byron Porter on 2023-05-25 05:51
  • Last Updated by Byron Porter on 2023-12-07 21:03
> 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
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_B_NB = 'Brand/Non-Brand'

# Check if RPT_COL_CAMPAIGN column has 'Brand' in the campaign name
bcheck = (inputDf[RPT_COL_CAMPAIGN].str.contains('Brand', case=False, 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-11-27 06:58:46 GMT

comments powered by Disqus