'string indices must be integers in a defaultdict, CSV File
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?
Solution 1:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | idanz |
