Script 589: Delete Duplicate Campaigns
Purpose
Python script that looks for duplicate campaigns created by Dynamic Campaigns and sets the status to “Deleted” for any older campaigns.
To Elaborate
The Python script solves the problem of identifying and handling duplicate campaigns created by Dynamic Campaigns. It specifically targets campaigns with the same name and sets the status to “Deleted” for any older campaigns. This helps to ensure that only the most recent campaigns are active and avoids confusion or redundancy in campaign management.
Walking Through the Code
- The script starts by getting the current date and time.
- It defines the primary data source and the columns used in the script.
- The script converts the ‘Campaign Creation Date’ column to datetime format for comparison purposes.
- The input DataFrame is sorted by ‘Performer Name’, ‘Performer ID’, and ‘Campaign Creation Date’ in descending order.
- The script identifies duplicates by using the
duplicated
function, excluding the most recent ‘Campaign Creation Date’. - A new DataFrame is created for the output, with columns for ‘Account’, ‘Campaign’, and ‘Status’.
- The relevant columns are copied from the duplicates DataFrame to the output DataFrame, and the ‘Status’ column is set to ‘Deleted’.
- The output DataFrame now contains only the campaigns marked as ‘Deleted’.
- The output DataFrame is printed to display the results.
Vitals
- Script ID : 589
- Client ID / Customer ID: 1306925293 / 60269377
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Status
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Jeff Sands (jsands@marinsoftware.com)
- Created by Jeff Sands on 2023-12-14 22:26
- Last Updated by Jeff Sands on 2024-01-11 21:49
> 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
##
## name: Delete Duplicate Campaigns
## description:
## Looks for Dynamic Campaigns-created campaigns with duplicate campaign names, sets the status to "Deleted" for any older campaigns.
##
## author:
## created: 2023-12-14
##
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_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_CAMPAIGN_CREATION_DATE = 'Campaign Creation Date'
RPT_COL_PERFORMER_ID = 'Performer ID'
RPT_COL_PERFORMER_NAME = 'Performer Name'
RPT_COL_LAST_TOUR_DATE = 'Last Tour Date'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_STATUS = 'Status'
# Convert 'Campaign Creation Date' to datetime for comparison
inputDf[RPT_COL_CAMPAIGN_CREATION_DATE] = pd.to_datetime(inputDf[RPT_COL_CAMPAIGN_CREATION_DATE])
# Sorting the DataFrame by 'Performer Name', 'Performer ID', and 'Campaign Creation Date' in descending order
inputDf = inputDf.sort_values(by=[RPT_COL_PERFORMER_NAME, RPT_COL_PERFORMER_ID, RPT_COL_CAMPAIGN_CREATION_DATE], ascending=[True, True, False])
# Using `duplicated` to select duplicates (except for the most recent 'Campaign Creation Date')
duplicates = inputDf[inputDf.duplicated(subset=[RPT_COL_PERFORMER_NAME, RPT_COL_PERFORMER_ID], keep='first')]
# Create a new DataFrame for output
outputDf = pd.DataFrame(columns=[BULK_COL_ACCOUNT, BULK_COL_CAMPAIGN, BULK_COL_STATUS])
# Copy relevant columns and set 'Status' to 'Deleted' for the selected duplicates
outputDf[BULK_COL_ACCOUNT] = duplicates[RPT_COL_ACCOUNT]
outputDf[BULK_COL_CAMPAIGN] = duplicates[RPT_COL_CAMPAIGN]
outputDf[BULK_COL_STATUS] = 'Deleted'
# Now outputDf contains only the campaigns marked as 'Deleted'
print(outputDf)
Post generated on 2024-05-15 07:44:05 GMT