Script 1769: Script Set Posting Status at Campaign Level
Purpose:
The script updates campaign-level posting status by mapping strategy-specific data from a reference dataset to an input dataset.
To Elaborate
The Python script is designed to update the posting status of campaigns by aligning data from an input dataset with a reference dataset based on specific strategies. The script uses a mapping mechanism to ensure that the correct posting values, such as daily budgets, CPA/ROAS targets, and bids, are assigned to each campaign. This is achieved by iterating over the input dataset and checking for matching strategies in the reference dataset. When a match is found, the corresponding values are updated in the output dataset. This process ensures that the campaign data is accurately reflected according to the predefined strategies, facilitating effective campaign management and budget allocation.
Walking Through the Code
- Data Preparation:
- The script begins by defining the primary and reference data sources, which are stored in
inputDf
andreportDf
respectively. These dataframes contain information about campaigns and their associated strategies.
- The script begins by defining the primary and reference data sources, which are stored in
- Column Mapping:
- A dictionary named
strategy_mapping
is created to map strategy-related columns from the reference dataset to the corresponding columns in the input dataset. This mapping is crucial for updating the correct fields in the output dataset.
- A dictionary named
- Data Iteration and Update:
- The script iterates over each row in the
inputDf
. For each campaign, it checks if the strategy exists in thereportDf
. If a matching strategy is found, the script updates the corresponding columns in the output dataset using the predefinedstrategy_mapping
.
- The script iterates over each row in the
- Output Generation:
- After processing all rows, the script prints the updated output dataframe, which now contains the updated posting status for each campaign based on the strategy mappings.
Vitals
- Script ID : 1769
- Client ID / Customer ID: 1306928641 / 60270613
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Posting Daily Budgets (DIM), Posting CPA/ROAS Targets (DIM), Posting Bids (DIM)
- Linked Datasource: M1 Report
- Reference Datasource: M1 Report
- Owner: ascott@marinsoftware.com (ascott@marinsoftware.com)
- Created by ascott@marinsoftware.com on 2025-02-28 18:49
- Last Updated by ascott@marinsoftware.com on 2025-02-28 18:51
> 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
##
## name: Script - Set Posting Status at Campaign Level
## description:
##
##
## author:
## created: 2025-02-28
##
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
# primary data source and columns
inputDf = dataSourceDict["1"]
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN_ID = 'Campaign ID'
RPT_COL_STRATEGY = 'Strategy'
RPT_COL_POSTING_BIDS_DIM = 'Posting Bids (DIM)'
RPT_COL_POSTING_CPA_PER_ROAS_TARGETS_DIM = 'Posting CPA/ROAS Targets (DIM)'
RPT_COL_POSTING_DAILY_BUDGETS_DIM = 'Posting Daily Budgets (DIM)'
# reference data source and columns
reportDf = dataSourceDict["2"] # report dataframe
RPT_COL_STRATEGY = 'Strategy'
RPT_COL_POSTING_DAILY_BUDGETS = 'Posting Daily Budgets'
RPT_COL_POSTING_CPA_PER_ROAS_TARGETS = 'Posting CPA/ROAS Targets'
RPT_COL_POSTING_BIDS = 'Posting Bids'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_POSTING_BIDS_DIM = 'Posting Bids (DIM)'
BULK_COL_POSTING_CPA_PER_ROAS_TARGETS_DIM = 'Posting CPA/ROAS Targets (DIM)'
BULK_COL_POSTING_DAILY_BUDGETS_DIM = 'Posting Daily Budgets (DIM)'
outputDf[BULK_COL_POSTING_BIDS_DIM] = "<<YOUR VALUE>>"
outputDf[BULK_COL_POSTING_CPA_PER_ROAS_TARGETS_DIM] = "<<YOUR VALUE>>"
outputDf[BULK_COL_POSTING_DAILY_BUDGETS_DIM] = "<<YOUR VALUE>>"
# Create mappings for strategy-to-column updates
strategy_mapping = {
RPT_COL_POSTING_DAILY_BUDGETS: RPT_COL_POSTING_DAILY_BUDGETS_DIM,
RPT_COL_POSTING_CPA_PER_ROAS_TARGETS: RPT_COL_POSTING_CPA_PER_ROAS_TARGETS_DIM,
RPT_COL_POSTING_BIDS: RPT_COL_POSTING_BIDS_DIM,
}
# Iterate over the input dataframe and update reference values based on strategy
for index, row in inputDf.iterrows():
strategy = row[RPT_COL_STRATEGY]
if strategy in reportDf[RPT_COL_STRATEGY].values:
matching_rows = reportDf[reportDf[RPT_COL_STRATEGY] == strategy]
for col, dim_col in strategy_mapping.items():
if not matching_rows.empty:
outputDf.loc[index, dim_col] = matching_rows.iloc[0][col]
# Display updated output dataframe
print(outputDf.head())
# user code start here
print(tableize(inputDf.head()))
Post generated on 2025-03-11 01:25:51 GMT