Script 1031: Tagging social dimension Audience and Segment

Purpose

Tagging social dimension Audience and Segment

To Elaborate

The Python script solves the problem of tagging the audience and segment of social media campaigns based on specific keywords in the campaign name. The script extracts the audience and segment from the campaign name, updates the corresponding columns in the output dataframe, and drops any rows with missing values.

Walking Through the Code

  1. The script defines configurable parameters for the keywords related to audience and segment, as well as the column names for campaign, account, audience, and segment.
  2. There is a function called “extract_audience_and_segment” that takes a campaign name as input and returns the extracted audience and segment based on the keywords.
  3. The input dataframe is copied to the output dataframe.
  4. The script iterates through each row of the input dataframe.
  5. For each row, it extracts the campaign name and calls the “extract_audience_and_segment” function to get the audience and segment.
  6. The campaign name, audience, and segment are printed.
  7. The audience and segment columns in the output dataframe are updated with the extracted values.
  8. Any rows with missing values in the audience and segment columns are dropped from the output dataframe.
  9. If the output dataframe is not empty, it is printed in a tabular format.
  10. If the output dataframe is empty, a message indicating that it is empty is printed.

Vitals

  • Script ID : 1031
  • Client ID / Customer ID: 1306927457 / 60270313
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Audience, Segment
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Autumn Archibald (aarchibald@marinsoftware.com)
  • Created by Autumn Archibald on 2024-04-30 00:09
  • Last Updated by Autumn Archibald on 2024-04-30 00:13
> 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
# Define the configurable parameters for the script
AUDIENCE_KEYWORDS = ['pricing', 'downloads', 'awv']
SEGMENT_KEYWORDS = ['b', 'nb', 'd', 'a', 'v', 'n']
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_AUDIENCE = 'Audience'
BULK_COL_SEGMENT = 'Segment'

# Function to extract audience and segment from campaign name
def extract_audience_and_segment(campaign_name):
    campaign_lower = campaign_name.lower()
    audience_list = [keyword for keyword in AUDIENCE_KEYWORDS if keyword in campaign_lower]
    segment_list = re.findall(r'\b(?:' + '|'.join(SEGMENT_KEYWORDS) + r')\b', campaign_lower)
    return ', '.join(audience_list), ', '.join(segment_list)

# Copy input rows to output
outputDf = inputDf.copy()

# Loop through all rows
for index, row in inputDf.iterrows():
    campaign_name = row[RPT_COL_CAMPAIGN]

    # Extract audience and segment from campaign name
    audience, segment = extract_audience_and_segment(campaign_name)
    print("Campaign [%s] => Audience [%s], Segment [%s]" % (campaign_name, audience, segment))

    # Update columns
    outputDf.at[index, BULK_COL_AUDIENCE] = audience
    outputDf.at[index, BULK_COL_SEGMENT] = segment

# Drop any rows with missing values
outputDf = outputDf.dropna(subset=[BULK_COL_AUDIENCE, BULK_COL_SEGMENT])

if not outputDf.empty:
    print("outputDf", tableize(outputDf))
else:
    print("Empty outputDf")

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

comments powered by Disqus