Script 133: Dimension Update
Purpose
Tags dimension as brand or non-brand.
To Elaborate
The Python script solves the problem of tagging a dimension as either brand or non-brand. It checks if a specific column in the input DataFrame contains the word “Brand” in the campaign name and creates two separate DataFrames for brand and non-brand tagging. It then merges these two DataFrames and outputs the result.
Walking Through the Code
- The script defines column constants for the campaign, account, and brand/non-brand columns.
- It checks if the campaign column contains the word “Brand” using the
str.contains()
method. - Based on the check, it creates two separate DataFrames: one for brand tagging and one for non-brand tagging.
- It sets the brand/non-brand column value to “Brand” for the brand DataFrame and “Non-Brand” for the non-brand DataFrame.
- The script merges the brand and non-brand DataFrames using the
merge()
method with an outer join. - It prints the tableized version of the output DataFrame using the
tableize()
function.
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-05-15 07:44:05 GMT