I have .csv file, about ramen and brands, varieties and ratings. I want to figure out, which Brand uses the Variety “Tom Yum” the most. I tried it with a defaultdict but i get the error code: string indices must be integers
This is my code so far:
from collections import defaultdict
tomyum = []
for row in liste:
if "Tom Yum" in row["Variety"]:
tomyum.append(row["Brand"])
d = defaultdict(int)
for row in tomyum:
for brand in row['Brand']:
d[brand] += 1
d
Anyone any Ideas?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
tomyum is a list of strings. Hence, in the following loop: for row in tomyum, row represents a string. That’s why you cannot do row['Brand'] in the next line, since you can access only indices in row, i.e. row[0], row[1], etc.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0