Script 1343: Dimension Update Groups

Purpose

Automates the process of updating ‘utmgroup’ dimensions based on ad group names by replacing spaces with plus signs.

To Elaborate

The Python script is designed to automate the process of updating the ‘utmgroup’ dimension in a dataset based on the names of ad groups. The script filters out rows where the ‘utmgroup’ is already populated, ensuring that only necessary updates are made. It then modifies the ad group names by replacing spaces with plus signs, which is a common practice for creating URL-friendly strings. This transformation is applied to the filtered dataset, and the updated values are stored in a new dataframe. The script is particularly useful for marketing and advertising data management, where consistent and structured naming conventions are crucial for tracking and analysis.

Walking Through the Code

  1. Data Preparation
    • The script begins by defining the primary data source and relevant columns, such as ‘Campaign’, ‘Account’, ‘Group’, and ‘utmgroup’.
    • It sets up the output columns to match the input columns, ensuring consistency in data structure.
  2. Function Definition
    • A function set_utm_group is defined to replace spaces in group names with plus signs. This function is crucial for transforming the ad group names into a URL-friendly format.
  3. Data Filtering
    • The script filters the input dataframe to exclude rows where the ‘utmgroup’ is already populated. This step ensures that only rows needing updates are processed.
  4. Data Transformation
    • The filtered dataframe is copied to a new dataframe, outputDf.
    • The set_utm_group function is applied to the ‘Group’ column of the filtered dataframe, and the results are stored in the ‘utmgroup’ column of the output dataframe.
  5. Output
    • The script concludes by printing the first few rows of the updated dataframe to verify the changes.

Vitals

  • Script ID : 1343
  • Client ID / Customer ID: 1306913420 / 60268008
  • Action Type: Bulk Upload
  • Item Changed: AdGroup
  • Output Columns: Account, Campaign, Group, utmgroup
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Kyle Perkins (kyle.perkins@genesys.com)
  • Created by Kyle Perkins on 2024-08-20 14:46
  • Last Updated by Kyle Perkins on 2024-08-28 17:18
> 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
##
## name: Dimension Update - Groups
## description:
##  
## 
## author: 
## created: 2024-08-20
## 

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_GROUP = 'Group'
RPT_COL_UTMGROUP = 'utmgroup'

# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_GROUP = 'Group'
BULK_COL_UTMGROUP = 'utmgroup'

# define function to replace ' ' with '+'
def set_utm_group(group_name):
    return group_name.replace(' ', '+')


# filter inputDf to skip rows where both utmgroup is populated and copy rows to new filteredDf dataframe
filteredDf = inputDf[
    ~inputDf[RPT_COL_UTMGROUP].notna()
].copy()

# intialize outputDf so that it only includes rows that have been filtered
outputDf = filteredDf

# apply the functions to each row in the filtered dataframe and set result as the output dataframe value
outputDf[BULK_COL_UTMGROUP] = filteredDf[RPT_COL_GROUP].apply(set_utm_group)

print(tableize(outputDf.head()))

Post generated on 2024-11-27 06:58:46 GMT

comments powered by Disqus