Count of category occurances (multiple cateogries can fall under each row) I want to get a count of each of the category occurrences (in how many rows did it show up in under the taxonomy column). The taxonomy column has a list of categories and I have several thousand rows, and I want to get a count of the number of times the categories shows up. Please note that there could be multiple categories per row but the same category will not be repeated in the same cell.

Is there a way to condense or clean the following code when running through a dataframe? I want to combine the if statements somehow without losing the logic.

for index, row in df.iterrows():
    for key in (row['taxonomy'] ) :


        if key in technical_issues_list:
            technical_issues += 1


        if key  ==    'technical_issues' :
            technical_issues_specific += 1

        if key == 'product_not_available':
            product_not_available += 1

        if key == 'site_performance':
            site_performance += 1 





        if key in checkout_list:
            checkout += 1

        if key == 'unable_to_checkout':
            unable_to_checkout += 1

        if key == 'unable_to_add_or_remove_products':
            unable_to_add_or_remove_products += 1

        if key == 'promotions':
            promotions += 1

        if key == 'payment_options':
            payment_options += 1

        if key == 'paypal':
            paypal += 1

        if key == 'shipping_cost':
            shipping_cost += 1

        if key == 'order_total_wrong':
            order_total_wrong += 1

        if key == 'backorder':
            backorder += 1

        if key == 'address_not_fitting_or_incorrect':
            address_not_fitting_or_incorrect +=1


        if key in fulfillment_list:
            fulfillment += 1


        if key == 'where_is_my_order':
            where_is_my_order += 1

        if key == 'BOPIS':
            BOPIS += 1 

        if key == 'delivery_issues':
            delivery_issues += 1

        if key == 'no_communication_or_delays':
            no_communication_or_delays += 1

        if key == 'damaged_product':
            damaged_product += 1

        if key == 'unassembled_assembly_issues':
            unassembled_assembly_issues += 1  




        if 'customer service' == key:
            customer_service += 1
        if 'long-wait_times' == key:
            long_wait_times +=1
        if 'contact_issues' == key:
            contact_issues +=1
        if 'canada_related'  == key:
            canada_related +=1
        if 'product + info' == key:
            product_info += 1
        if 'design issues' == key:
            design_issues += 1
        if 'gift_wrapping' in key:
            gift_wrapping +=1
        if  'stores' == key:
            store += 1
        if  'email' == key:
            email += 1
        if  'suggestions' == key:
            suggestions +=1
        if 'rewards' == key:
            rewards +=1
        if  'registry' == key:
            registry += 1
        if  'returns_cancellations' in key:
            returns_cancellations +=1
All I did was convert all if statements with exact values into a dictionary. This way will allow you to increment the values of each key without as much if/else usage. If you then want to later on pass the values of each key to a variable of your choice, you can just loop through the dictionary once, which is less time consuming and it also makes debugging easier.

I didn't touch the if statements using in. You can try placing it in the dict too.

all_dict = {'customer service':0,
    'long-wait_times':0,
    'contact_issues':0,
    'canada_related':0,
    'product + info':0,
    'design issues':0,
    'gift_wrapping':0,
    'stores':0,
    'email' :0,
    'suggestions':0,
    'rewards':0,
    'registry' :0,
    'returns_cancellations':0
    'technical_issues':0,
    'product_not_available':0,
    'site_performance':0,
    'unable_to_checkout':0,
    'unable_to_add_or_remove_products':0,
    'promotions':0,
    'payment_options':0,
    'paypal':0,
    'shipping_cost':0,
    'order_total_wrong':0,
    'backorder':0,
    'address_not_fitting_or_incorrect':0,
    'where_is_my_order':0,
    'BOPIS':0,
    'delivery_issues':0,
   'no_communication_or_delays':0,
   'damaged_product':0,
  'unassembled_assembly_issues':0
  }

for index, row in df.iterrows():
    for key in (row['taxonomy'] ) :

        if key in technical_issues_list:
            technical_issues += 1

        elif key in checkout_list:
            checkout += 1

        elif key in fulfillment_list:
            fulfillment += 1

        elif  'returns_cancellations' in key:
            returns_cancellations +=1

        else:
            all_dict[key] += 1