Script 827: Cross Negate Single Keyword Campaigns

Purpose

Generating negative keywords for each account in a campaign.

To Elaborate

The Python script solves the problem of generating negative keywords for each account in a campaign. Negative keywords are used in online advertising campaigns to prevent ads from being shown when certain keywords are searched. This script takes an input dataframe that contains information about keywords, accounts, campaigns, and groups, and generates a list of negative keywords for each account. The negative keywords are generated by comparing each keyword with all other keywords in the same account, and appending the other keywords as negative keywords for the current keyword.

Walking Through the Code

  1. The script defines column names for the input dataframe.
  2. The script defines a function called generate_negative_keywords that takes an input dataframe as a parameter.
  3. Inside the function, an empty list called negative_keywords_by_account is initialized to store negative keywords for each account.
  4. The input dataframe is grouped by account using the groupby function.
  5. The script iterates over each group (account) in the grouped data.
  6. Inside the loop, an empty list called negative_keywords_for_account is initialized to store negative keywords for the current account.
  7. The script iterates over each row in the current group (account).
  8. Inside the inner loop, the current keyword is extracted from the row.
  9. All other keywords in the account except the current one are obtained using a filter and stored in the other_keywords variable.
  10. The script appends each other keyword as a negative keyword for the current keyword by creating a dictionary with the keyword information and appending it to the negative_keywords_for_account list.
  11. After the inner loop, the negative_keywords_for_account list is extended to the negative_keywords_by_account list.
  12. The function returns the negative_keywords_by_account list.
  13. The script calls the generate_negative_keywords function with the inputDf dataframe and assigns the result to the all_negative_keywords variable.
  14. The script prints each negative keyword information using a loop over the all_negative_keywords list.

Vitals

  • Script ID : 827
  • Client ID / Customer ID: 1306922797 / 60269073
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Keyword
  • Output Columns: Account, Campaign, Group, Keyword, Match Type, Status
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
  • Created by Grégory Pantaine on 2024-03-20 12:07
  • Last Updated by Grégory Pantaine on 2024-03-20 12:16
> 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
# Define report column names
RPT_COL_KEYWORD = 'Keyword'
RPT_COL_STATUS = 'Status'
RPT_COL_MATCH_TYPE = 'Match Type'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_GROUP = 'Group'

# Function to generate negative keywords for each account
def generate_negative_keywords(input_df):
    # Initialize an empty list to store negative keywords for each account
    negative_keywords_by_account = []

    # Group data by account
    grouped_data = input_df.groupby(RPT_COL_ACCOUNT)
    
    # Iterate over each group (account)
    for account, group in grouped_data:
        # Initialize an empty list to store negative keywords for the current account
        negative_keywords_for_account = []

        # Iterate over each row in the group (account)
        for index, row in group.iterrows():
            # Get the current keyword
            current_keyword = row[RPT_COL_KEYWORD]

            # Get all other keywords in the account except the current one
            other_keywords = group[group[RPT_COL_KEYWORD] != current_keyword][RPT_COL_KEYWORD].unique()

            # Append other keywords as negative keywords for the current keyword
            for other_keyword in other_keywords:
                negative_keywords_for_account.append({
                    'Negative Keyword': other_keyword,
                    'Account': account,
                    'Campaign': row[RPT_COL_CAMPAIGN],
                    'Group': row[RPT_COL_GROUP],
                    'Match Type': 'Exact'
                })
        
        # Extend the list of negative keywords for the account
        negative_keywords_by_account.extend(negative_keywords_for_account)

    return negative_keywords_by_account

# Generate negative keywords for all accounts
all_negative_keywords = generate_negative_keywords(inputDf)

# Print the negative keywords
for keyword_info in all_negative_keywords:
    print(f"Negative Keyword: {keyword_info['Negative Keyword']}, Account: {keyword_info['Account']}, Campaign: {keyword_info['Campaign']}, Group: {keyword_info['Group']}, Match Type: {keyword_info['Match Type']}")

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

comments powered by Disqus