Script 541: Dimension AutoApply Brand NonBrand

Purpose

Tag Brand vs Non Brand dimension automatically

To Elaborate

The Python script automates the process of tagging the “Brand vs NonBrand” dimension in a report. It identifies campaigns that contain the word “Brand” and tags them as “Brand”, while campaigns that do not contain the word “Brand” are tagged as “NonBrand”. The script then updates the output file with the new tags and includes only the campaigns that have a changed tag.

Walking Through the Code

  1. The script defines column constants for the report and bulk file.
  2. The current date is stored in the variable “today”.
  3. The input DataFrame is printed in a table format.
  4. A temporary field is created in the input DataFrame to store the new tags.
  5. Campaigns containing the word “Brand” are tagged as “Brand” in the temporary field.
  6. Campaigns not containing the word “Brand” are tagged as “NonBrand” in the temporary field.
  7. The input DataFrame is printed again in a table format.
  8. The script checks for duplicated indices in the input DataFrame.
  9. The new tags from the temporary field are copied to the output DataFrame.
  10. The output DataFrame is filtered to include only campaigns with a changed tag.
  11. The updated output DataFrame is returned.

Vitals

  • Script ID : 541
  • Client ID / Customer ID: 1306912103 / 60267913
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Brand vs NonBrand
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
  • Created by Grégory Pantaine on 2023-11-21 16:44
  • 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
28
29
30
31
32
33
34
35
36
37
# Automating "Brand vs NonBrand" Dimension Tagging
# script name: Dimension_AutoApply_Brand_NonBrand
# base report name: Default_DimensionTagging_BrandNonBrand
# Taken from Byron Porter's setup, Marin Software PS team
# setup 2023-11-21 by Greg Pantaine, CE on account

RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_BRAND_VS_NONBRAND = 'Brand vs NonBrand'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_BRAND_VS_NONBRAND = 'Brand vs NonBrand'

#outputDf[BULK_COL_BRAND_VS_NONBRAND] = "<<YOUR VALUE>>"

today = datetime.datetime.now(CLIENT_TIMEZONE).date()
print(tableize(inputDf))



TMP_FIELD = BULK_COL_BRAND_VS_NONBRAND + '_new'
# blank out tmp field
inputDf[TMP_FIELD] = numpy.nan

inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('Brand', case=False)) , TMP_FIELD ] = 'Brand'
inputDf.loc[~inputDf[RPT_COL_CAMPAIGN].str.contains('Brand', case=False), TMP_FIELD] = 'NonBrand'


print(tableize(inputDf))

print(inputDf.index.duplicated())

# copy new strategy to output
outputDf.loc[:,BULK_COL_BRAND_VS_NONBRAND] = inputDf.loc[:, TMP_FIELD]

# only include campaigns with changed strategy in bulk file
outputDf = outputDf[ inputDf[TMP_FIELD].notnull() & (inputDf[BULK_COL_BRAND_VS_NONBRAND] != inputDf[TMP_FIELD]) ]

Post generated on 2024-05-15 07:44:05 GMT

comments powered by Disqus