# 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)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter