Script 1003: Tagging dimension Script Campaigns F YY Brand
Purpose
Python script to extract the F-YY-Brand from campaign names and update the F-YY-Brand column in the output dataframe.
To Elaborate
The Python script solves the problem of extracting the F-YY-Brand from campaign names and updating the F-YY-Brand column in the output dataframe. The script defines configurable parameters such as the separator used in campaign names and the location of the F-YY-Brand tag. It then iterates through each row of the input dataframe, extracts the F-YY-Brand from the campaign name using the specified separator and tag location, and updates the F-YY-Brand column in the output dataframe. Any rows with missing F-YY-Brand values are dropped from the output dataframe.
Walking Through the Code
- The script defines configurable parameters such as the separator used in campaign names (SEP) and the location of the F-YY-Brand tag (TAG_LOCATION).
- The function
extract_fyybrand(campaign_name)
is defined to extract the F-YY-Brand from the campaign name. It splits the campaign name using the separator and returns the first word before the first separator. If no separator is found, it returns the whole campaign name as the F-YY-Brand. - The input dataframe is copied to the output dataframe.
- The script iterates through each row of the input dataframe using
iterrows()
. - For each row, it extracts the campaign name from the RPT_COL_CAMPAIGN column.
- It calls the
extract_fyybrand(campaign_name)
function to extract the F-YY-Brand from the campaign name. - The F-YY-Brand value is printed for each campaign.
- The F-YY-Brand column in the output dataframe is updated with the extracted F-YY-Brand value using
outputDf.at[index, BULK_COL_FYYBRAND] = fyybrand
. - Any rows with missing F-YY-Brand values are dropped from the output dataframe using
outputDf = outputDf.dropna(subset=[BULK_COL_FYYBRAND])
. - If the output dataframe is not empty, it is printed using the
tableize()
function. Otherwise, “Empty outputDf” is printed.
Vitals
- Script ID : 1003
- Client ID / Customer ID: 1306927457 / 60270313
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, F-YY-Brand
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Autumn Archibald (aarchibald@marinsoftware.com)
- Created by Autumn Archibald on 2024-04-25 20:47
- Last Updated by Autumn Archibald on 2024-04-25 21:38
> 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
# Define the configurable parameters for the script
SEP = '_' # Separator used in campaign names
TAG_LOCATION = 0 # First word before the first separator
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_FYYBRAND = 'F-YY-Brand'
# Function to extract F-YY-Brand from campaign name
def extract_fyybrand(campaign_name):
if SEP in campaign_name:
return campaign_name.split(SEP)[TAG_LOCATION] # Extracting the first word before the first separator "_"
else:
return campaign_name # If no separator is found, consider the whole campaign name as F-YY-Brand
# 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 F-YY-Brand from campaign name
fyybrand = extract_fyybrand(campaign_name)
print("Campaign [%s] => F-YY-Brand [%s]" % (campaign_name, fyybrand))
# Update F-YY-Brand column
outputDf.at[index, BULK_COL_FYYBRAND] = fyybrand
# Drop any rows with missing F-YY-Brand values
outputDf = outputDf.dropna(subset=[BULK_COL_FYYBRAND])
if not outputDf.empty:
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
Post generated on 2024-05-15 07:44:05 GMT