Script 621: Pause CCT Groups

Purpose

Python script to pause CCT groups based on specific conditions.

To Elaborate

This Python script solves the problem of pausing CCT (Campaign, Creative, and Targeting) groups in a data source based on certain conditions. The script checks if the ‘Studio’ column has specific values and if the ‘Group’ column contains a specific phrase. If both conditions are met, the script updates the ‘Status’ column of the corresponding group to ‘PAUSED - ANIME’. The script also checks if a keyword in the ‘Keyword’ column consists of a single word, and if so, updates the ‘CCT_Single_keyword_processing’ column to ‘Paused|Single Word|'. The script then merges the updated group data with the keyword data and applies additional checks. Finally, the script updates the 'CCT_sourcetemplate' and 'CCT_Group_pause_check' columns based on the results of the checks.

Walking Through the Code

  1. The script starts by importing the necessary libraries and defining the ‘today’ variable.
  2. The script defines the columns used in the input and output dataframes.
  3. The script initializes the output dataframe with initial values for the relevant columns.
  4. The script prints the tableized version of the input dataframe.
  5. The script defines a function called ‘check_token_length’ that checks if a text consists of a single word or not.
  6. The script defines a function called ‘check_conditions’ that checks if specific conditions are met for each row in the dataframe.
  7. The script creates a new dataframe called ‘checkDf’ by selecting specific columns from the input dataframe.
  8. The script applies the ‘check_token_length’ function to the ‘RPT_COL_KEYWORD’ column of ‘checkDf’ and updates the ‘BULK_COL_STATUS_UPDATE’ column accordingly.
  9. The script updates the ‘BULK_COL_CCT_SINGLE_KEYWORD_PROCESSING’ column of ‘checkDf’ for rows where ‘BULK_COL_STATUS_UPDATE’ is ‘PAUSED’.
  10. The script prints the tableized version of ‘checkDf’.
  11. The script filters ‘checkDf’ to only include rows where ‘BULK_COL_STATUS_UPDATE’ is ‘PAUSED’.
  12. The script creates a new dataframe called ‘dedupeGroupDf’ by selecting specific columns from the input dataframe and removing duplicate rows based on ‘BULK_COL_ACCOUNT’, ‘BULK_COL_CAMPAIGN’, and ‘BULK_COL_GROUP’.
  13. The script prints the tableized version of ‘dedupeGroupDf’.
  14. The script merges ‘dedupeGroupDf’ and ‘checkDf’ on the specified columns and stores the result in ‘merged_df’.
  15. The script updates the ‘BULK_COL_STATUS’ column of ‘merged_df’ with the values from ‘RPT_COL_GROUP_STATUS’.
  16. The script updates the ‘BULK_COL_CCT_SINGLE_KEYWORD_PROCESSING’ column of ‘merged_df’ to ‘Checked’ for rows where ‘BULK_COL_STATUS_UPDATE’ is not ‘PAUSED’.
  17. The script creates a backup of the ‘BULK_COL_CCT_SOURCETEMPLATE’ column in ‘merged_df’.
  18. The script updates the ‘BULK_COL_CCT_SOURCETEMPLATE’ column of ‘merged_df’ to an empty string for rows where ‘BULK_COL_STATUS_UPDATE’ is ‘PAUSED’.
  19. The script updates the ‘BULK_COL_STATUS’ column of ‘merged_df’ to ‘PAUSED’ for rows where ‘BULK_COL_STATUS_UPDATE’ is ‘PAUSED’.
  20. The script applies the ‘check_conditions’ function to ‘merged_df’.
  21. The script updates the ‘BULK_COL_CCT_SOURCETEMPLATE’ column of ‘merged_df’ to an empty string for rows where ‘anime_status’ is ‘PAUSED - ANIME’.
  22. The script updates the ‘BULK_COL_STATUS’ column of ‘merged_df’ to ‘PAUSED’ for rows where ‘anime_status’ is ‘PAUSED - ANIME’.
  23. The script updates the ‘BULK_COL_CCT_GROUP_PAUSE_CHECK’ column of ‘merged_df’ to ‘Paused Anime ' for rows where 'anime_status' is 'PAUSED - ANIME'.
  24. The script updates the ‘BULK_COL_CCT_GROUP_PAUSE_CHECK’ column of ‘merged_df’ to ‘Checked’ for rows where ‘anime_status’ is not ‘PAUSED - ANIME’.
  25. The script assigns ‘merged_df’ to ‘outputDf’.

Vitals

  • Script ID : 621
  • Client ID / Customer ID: 1306924345 / 69058
  • Action Type: Bulk Upload
  • Item Changed: AdGroup
  • Output Columns: Account, Campaign, Group, Status, CCT_Single_keyword_processing, CCT_Group_pause_check, CCT_SourceTemplate_Backup, CCT_sourcetemplate
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Jonathan Reichl (jreichl@marinsoftware.com)
  • Created by Jonathan Reichl on 2023-12-22 14:32
  • Last Updated by Jonathan Reichl on 2024-01-15 15:24
> 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
##
## name: Pause CCT Groups
## description:
##  
## 
## author: 
## created: 2023-12-22
## 

today = datetime.datetime.now(CLIENT_TIMEZONE).date()

# primary data source and columns
inputDf = dataSourceDict["1"]
RPT_COL_KEYWORD = 'Keyword'
RPT_COL_PUBLISHER = 'Publisher'
RPT_COL_CCT_SOURCETEMPLATE = 'CCT_sourcetemplate'
RPT_COL_CCT_SINGLE_KEYWORD_PROCESSING = 'CCT_Single_keyword_processing'
RPT_COL_CCT_GROUP_PAUSE_CHECK = 'CCT_Group_pause_check'
RPT_COL_CCT_SOURCETEMPLATE_BACKUP = 'CCT_SourceTemplate_Backup'
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_GROUP = 'Group'
RPT_COL_PUB_ID = 'Pub. ID'
RPT_COL_GROUP_PUB_ID = 'Group Pub. ID'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_STUDIO = 'Studio'
RPT_COL_GROUP_STATUS = 'Group Status'

# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_GROUP = 'Group'
BULK_COL_CCT_GROUP_PAUSE_CHECK = 'CCT_Group_pause_check'
BULK_COL_CCT_SINGLE_KEYWORD_PROCESSING = 'CCT_Single_keyword_processing'
BULK_COL_CCT_SOURCETEMPLATE = 'CCT_sourcetemplate'
BULK_COL_CCT_SOURCETEMPLATE_BACKUP = 'CCT_SourceTemplate_Backup'
BULK_COL_STATUS_UPDATE = 'updated_status'
BULK_COL_STATUS = 'Status'
outputDf[BULK_COL_CCT_GROUP_PAUSE_CHECK] = "<<YOUR VALUE>>"
outputDf[BULK_COL_CCT_SINGLE_KEYWORD_PROCESSING] = "<<YOUR VALUE>>"
outputDf[BULK_COL_CCT_SOURCETEMPLATE] = "<<YOUR VALUE>>"
outputDf[BULK_COL_CCT_SOURCETEMPLATE_BACKUP] = "<<YOUR VALUE>>"

# user code start here
print(tableize(inputDf))

def check_token_length(text):
    tokens = text.split()  # Splitting based on spaces
    if len(tokens) == 1:
        return 'PAUSED'
    else:
        return 'unchanged'

def check_conditions(df):
    # Check if 'RPT_COL_STUDIO' is in (x, y, z)
    studio_values = ['Medialink', 'Muse', 'Studio Pierrot', 'Liden Films']
    studio_check = df[RPT_COL_STUDIO].isin(studio_values)
    
    # Check if 'phrase' (case insensitive) is in 'RPT_COL_GROUP'
    phrase_check = df[RPT_COL_GROUP].str.contains('phrase', case=False, na=False)
    
    # If both conditions are met, return 'PAUSED - ANIME'
    df.loc[studio_check & phrase_check, 'anime_status'] = 'PAUSED - ANIME'


#Build keyword check DF 
check_columns = [BULK_COL_ACCOUNT,BULK_COL_CAMPAIGN,BULK_COL_GROUP,RPT_COL_KEYWORD]
checkDf = inputDf[check_columns].copy()

#where only a single work set to puased 
checkDf[BULK_COL_STATUS_UPDATE] = checkDf[RPT_COL_KEYWORD].apply(check_token_length)


# set  BULK_COL_CCT_Single_word_check for paused objects 
checkDf.loc[checkDf[BULK_COL_STATUS_UPDATE] == 'PAUSED' , BULK_COL_CCT_SINGLE_KEYWORD_PROCESSING] = 'Paused|Single Word|'+ str(today)


print(tableize(checkDf))

# Filter checkDf to only include paused objects e.g. single keywords 
checkDf =  checkDf[checkDf[BULK_COL_STATUS_UPDATE] == 'PAUSED']


# build group DF 
group_columns = [BULK_COL_ACCOUNT,BULK_COL_CAMPAIGN,BULK_COL_GROUP, RPT_COL_CCT_SOURCETEMPLATE, RPT_COL_GROUP_STATUS, RPT_COL_STUDIO] 
dedupeGroupDf = inputDf[group_columns].copy()

#one row per group 
dedupeGroupDf = dedupeGroupDf.drop_duplicates(subset=[BULK_COL_ACCOUNT,BULK_COL_CAMPAIGN,BULK_COL_GROUP], keep='first')


print('dedupeGroupDf')
print(tableize(dedupeGroupDf))

#merge 2 DFs 
merged_df = pd.merge(dedupeGroupDf, checkDf, on=[BULK_COL_ACCOUNT, BULK_COL_CAMPAIGN, BULK_COL_GROUP], how='left')
print('merged_df')
print(tableize(merged_df))

merged_df[BULK_COL_STATUS] = merged_df[RPT_COL_GROUP_STATUS]

merged_df.loc[merged_df[BULK_COL_STATUS_UPDATE] != 'PAUSED' , BULK_COL_CCT_SINGLE_KEYWORD_PROCESSING] = 'Checked'

merged_df[BULK_COL_CCT_SOURCETEMPLATE_BACKUP] = merged_df[BULK_COL_CCT_SOURCETEMPLATE]

merged_df.loc[merged_df[BULK_COL_STATUS_UPDATE] == 'PAUSED' , BULK_COL_CCT_SOURCETEMPLATE] = ''

merged_df.loc[merged_df[BULK_COL_STATUS_UPDATE] == 'PAUSED' , BULK_COL_STATUS] = 'PAUSED'


#apply anime check
check_conditions(merged_df) 

merged_df.loc[merged_df['anime_status'] == 'PAUSED - ANIME' , BULK_COL_CCT_SOURCETEMPLATE] = ''

merged_df.loc[merged_df['anime_status'] == 'PAUSED - ANIME' , BULK_COL_STATUS] = 'PAUSED'

merged_df.loc[merged_df['anime_status'] == 'PAUSED - ANIME' , BULK_COL_CCT_GROUP_PAUSE_CHECK] = 'Paused|Anime|'+str(today)

merged_df.loc[merged_df['anime_status'] != 'PAUSED - ANIME' , BULK_COL_CCT_GROUP_PAUSE_CHECK] = 'Checked'


outputDf = merged_df

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

comments powered by Disqus