Script 259: Dimension Update
Purpose
Python script to assign campaign dimension values based on strategy/targeting type and campaign tactic.
To Elaborate
The Python script solves the problem of assigning campaign dimension values based on the strategy/targeting type and campaign tactic. It takes an input dataframe that contains campaign information and extracts the strategy/targeting type and campaign tactic from the campaign name using regular expressions. It then assigns these values to the respective dimensions in the output dataframe. The script also identifies any campaigns that have changed dimension values and outputs them in a separate dataframe.
Walking Through the Code
- The script defines column constants for the input and output dataframes.
- It defines regular expression patterns for matching the strategy/targeting type and campaign tactic in the campaign name.
- The script extracts the strategy/targeting type and campaign tactic from the campaign name using the regular expression patterns and assigns them to the respective columns in the input dataframe.
- It removes a temporary column used for extraction.
- The script identifies campaigns that have changed dimension values by checking if the strategy/targeting type or campaign tactic columns are not empty.
- If there are changed campaigns, it prints the campaigns with updated dimension values and creates an output dataframe with only the changed rows.
- The script renames the columns in the output dataframe to match the column constants for the output dataframe.
- If there are no changed campaigns, it prints an empty output dataframe.
- The script ends.
Vitals
- Script ID : 259
- Client ID / Customer ID: 1306922481 / 2
- Action Type: Bulk Upload (Preview)
- Item Changed: Campaign
- Output Columns: Account, Campaign, Strategy//Targeting Type, Campaign Tactic
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Jeremy Brown (jbrown@marinsoftware.com)
- Created by Jeremy Brown on 2023-07-27 16:04
- Last Updated by vapalussiere@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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# Assign Campaign Dimension Values according to:
# - Strategy//Targeting Type
# - Campaign Tactic
#
# Author: Jeremy Brown
# Date: 2023-07-27
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_STRATEGY_TARGETING_TYPE = 'Strategy//Targeting Type'
RPT_COL_CAMPAIGN_TACTIC = 'Campaign Tactic'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_STRATEGY_TARGETING_TYPE = 'Strategy-Targeting Type'
BULK_COL_CAMPAIGN_TACTIC = 'Campaign Tactic'
#outputDf[BULK_COL_STRATEGY_TARGETING_TYPE] = "TACTIC"
#outputDf[BULK_COL_CAMPAIGN_TACTIC] = "COST_MODEL"
# define pattern match
PATTERN1 = r'(ISA|ISG|AWR|TRF|YTB|IGR|TIK|YTS)'
PATTERN2 = r'(?:CPV_)?(CPM|CPV|CPC)'
# find all occurrences of the pattern in the Campaign name
inputDf[RPT_COL_STRATEGY_TARGETING_TYPE] = inputDf[RPT_COL_CAMPAIGN].str.extract(PATTERN1).fillna(' ')
inputDf['TEMP_COL'] = inputDf[RPT_COL_CAMPAIGN].str.replace('VID','CPV')
inputDf[RPT_COL_CAMPAIGN_TACTIC] = inputDf['TEMP_COL'].str.extract(PATTERN2).fillna(' ')
del inputDf['TEMP_COL']
# define tmp column for dimension 'Strategy//Targeting Type' and set to empty
#TMP_TYPE = RPT_COL_STRATEGY_TARGETING_TYPE + '_'
#inputDf[TMP_TYPE] = np.nan
# define tmp column for dimension 'Campaign Tactic' and set to empty
#TMP_TACTIC = RPT_COL_CAMPAIGN_TACTIC + '_'
#inputDf[TMP_TACTIC] = np.nan
# Assign the matches to the respective dimensions based on matches for PATTERN1
#inputDf.loc[inputDf[RPT_COL_CAMPAIGN].apply(lambda x: bool(re.findall(PATTERN1, x))), TMP_TYPE] = MATCH1
# Assign the matches to the respective dimensions based on matches for PATTERN2
#inputDf.loc[inputDf[RPT_COL_CAMPAIGN].apply(lambda x: bool(re.findall(PATTERN2, x))), TMP_TACTIC] = MATCH2
# find changed campaigns
changed = inputDf[RPT_COL_STRATEGY_TARGETING_TYPE].ne(' ') | inputDf[RPT_COL_CAMPAIGN_TACTIC].ne(' ')
# put changed campaigns into outputDf; if none, prepare empty outputDf
if sum(changed) > 0:
print("== Campaigns with updated Dimension values ==", tableize(inputDf.loc[changed]))
# only select changed rows
#cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_STRATEGY_TARGETING_TYPE, RPT_COL_CAMPAIGN_TACTIC]
#outputDf = inputDf.loc[ changed, cols ].copy() \
# .rename(columns = { \
# TMP_TYPE: BULK_COL_STRATEGY_TARGETING_TYPE, \
# TMP_TACTIC: BULK_COL_CAMPAIGN_TACTIC \
# })
outputDf = inputDf.loc[changed]
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
outputDf = outputDf.iloc[0:0]
Post generated on 2024-05-15 07:44:05 GMT