Iterating over rows and counting the occurrences of ingredients listed in the 'TopNotes' column

PHOTO EMBED

Tue May 21 2024 15:38:33 GMT+0000 (Coordinated Universal Time)

Saved by @Uncoverit #python

# Iterating over rows and counting the occurrences of ingredients listed in the 'TopNotes' column 
top_ingredient_counts = {}

for index, row in df.iterrows():
    top_notes_list = row['TopNotes']
    if isinstance(top_notes_list, list):
        for ingredient in top_notes_list:
            top_ingredient_counts[ingredient] = top_ingredient_counts.get(ingredient, 0) + 1
    elif isinstance(top_notes_list, str):
        ingredients = [x.strip() for x in top_notes_list.split(',')]
        for ingredient in ingredients:
            top_ingredient_counts[ingredient] = top_ingredient_counts.get(ingredient, 0) + 1
top_ingredient_series = pd.Series(top_ingredient_counts, dtype=int)
print(top_ingredient_series)
content_copyCOPY