Script 1345: PPC Grade

Purpose

The Python script categorizes PPC scores into letter grades ranging from A to D based on predefined score ranges.

To Elaborate

The Python script is designed to categorize Pay-Per-Click (PPC) scores into letter grades, which are A, B, C, and D. This categorization is based on specific score ranges, allowing for a structured evaluation of PPC performance. The script processes data from a primary data source, applies a function to determine the grade for each PPC score, and outputs the results with the necessary columns. This categorization helps in quickly assessing the effectiveness of PPC campaigns by assigning a straightforward grade that reflects the score’s quality.

Walking Through the Code

  1. Data Preparation
    • The script begins by defining the primary data source and the relevant columns needed for processing, such as ‘Campaign’, ‘Account’, ‘Campaign ID’, and ‘PPC Score’.
  2. Defining the Grading Function
    • A function get_bulk_col_ppc_grade is defined to categorize PPC scores into grades:
      • Scores between 4 and 12 are graded as “A”.
      • Scores between 13 and 18 are graded as “B”.
      • Scores between 19 and 22 are graded as “C”.
      • Scores between 23 and 28 are graded as “D”.
      • Scores outside these ranges receive an empty string.
  3. Applying the Grading Function
    • The function is applied to the ‘PPC Score’ column of the input DataFrame to calculate the corresponding PPC grade for each entry.
  4. Output Preparation
    • The script selects the necessary columns for output, including ‘Account’, ‘Campaign’, ‘Campaign ID’, and the newly calculated ‘PPC Grade’.
    • It then renames the columns to ensure consistency and clarity in the output DataFrame.
  5. Displaying Results
    • Finally, the script prints the first few rows of the output DataFrame to display the categorized PPC grades.

Vitals

  • Script ID : 1345
  • Client ID / Customer ID: 1306926629 / 60270083
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Campaign ID, PPC Grade
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: ascott@marinsoftware.com (ascott@marinsoftware.com)
  • Created by ascott@marinsoftware.com on 2024-08-23 13:01
  • Last Updated by ascott@marinsoftware.com on 2024-09-06 19:17
> 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
## name: Set PPC Rank
## description:
##  This Script categorizes PPC scores into letter scores A - D
## 
## author: Adam Scott
## created: 8/22/2024
## 

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_PPC_SCORE = 'PPC Score'

# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_CAMPAIGN_ID = 'Campaign ID'
BULK_COL_PPC_GRADE = 'PPC Grade'

# Caclcuate PPC Rank based on PPC Score
def get_bulk_col_ppc_grade(rpt_col_ppc_score):
    if 4 <= rpt_col_ppc_score <= 12:
        return "A"
    elif 13 <= rpt_col_ppc_score <= 18:
        return "B"
    elif 19 <= rpt_col_ppc_score <= 22:
        return "C"
    elif 23 <= rpt_col_ppc_score <= 28:
        return "D"
    else:
        return ""

# Apply the function to calculate PPC Rank
inputDf[BULK_COL_PPC_GRADE] = inputDf[RPT_COL_PPC_SCORE].apply(get_bulk_col_ppc_grade)

# Select required columns for output
outputDf = inputDf[[RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_CAMPAIGN_ID, BULK_COL_PPC_GRADE]]

# Rename columns
outputDf.columns = [BULK_COL_ACCOUNT, BULK_COL_CAMPAIGN, BULK_COL_CAMPAIGN_ID, BULK_COL_PPC_GRADE]

# Print output DataFrame
print(outputDf.head())

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

comments powered by Disqus