Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading