Script 161: populate market
Purpose
The script assigns market identifiers to campaigns based on specific patterns in campaign names.
To Elaborate
The Python script is designed to populate the Market dimension of a dataset by assigning market identifiers to campaigns. It does this by examining the campaign names and looking for specific patterns that indicate the market. For example, if a campaign name contains ‘- AUS -‘, it assigns ‘AUS’ as the market identifier. This process is repeated for several predefined market codes such as ‘FR’, ‘IRE’, ‘SA’, ‘UAE’, ‘UK’, and ‘US’. The script ensures that only campaigns with a changed market strategy are included in the final output, which helps in maintaining an accurate and updated market allocation for each campaign.
Walking Through the Code
- Initialization and Setup
- The script begins by defining constants for column names used in both reporting and bulk data.
- A temporary field is created in the input DataFrame to store new market identifiers, initialized with
NaN
values.
- Market Identification
- The script checks each campaign name for specific substrings that correspond to different markets (e.g., ‘- AUS -‘ for Australia).
- When a match is found, the corresponding market code is assigned to the temporary field in the DataFrame.
- Data Processing and Output Preparation
- The script identifies and prints any duplicated indices in the input DataFrame.
- It then copies the newly assigned market identifiers from the temporary field to the output DataFrame.
- Finally, it filters the output to include only those campaigns where the market strategy has changed, ensuring that only relevant updates are processed.
Vitals
- Script ID : 161
- Client ID / Customer ID: 1306922915 / 60269133
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Market
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Jonathan Reichl (jreichl@marinsoftware.com)
- Created by Jonathan Reichl on 2023-06-05 08:46
- Last Updated by Jonathan Reichl 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
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_MARKET = 'Market'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_MARKET = 'Market'
#outputDf[BULK_COL_MARKET] = "<<YOUR VALUE>>"
TMP_FIELD = BULK_COL_MARKET + '_new'
# blank out tmp field
inputDf[TMP_FIELD] = numpy.nan
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
print(tableize(inputDf))
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('- AUS -', case=False)) , TMP_FIELD ] = 'AUS'
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('- FR -', case=False)) , TMP_FIELD ] = 'FR'
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('- IRE -', case=False)) , TMP_FIELD ] = 'IRE'
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('- SA -', case=False)) , TMP_FIELD ] = 'SA'
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('- UAE -', case=False)) , TMP_FIELD ] = 'UAE'
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('- UK -', case=False)) , TMP_FIELD ] = 'UK'
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('- US -', case=False)) , TMP_FIELD ] = 'US'
print(tableize(inputDf))
print(inputDf.index.duplicated())
# copy new strategy to output
outputDf.loc[:,BULK_COL_MARKET] = inputDf.loc[:, TMP_FIELD]
# only include campaigns with changed strategy in bulk file
outputDf = outputDf[ inputDf[TMP_FIELD].notnull() & (inputDf[BULK_COL_MARKET] != inputDf[TMP_FIELD]) ]
Post generated on 2024-11-27 06:58:46 GMT