Reputation: 5
I am kind of lost here and can't really find a similar question. Maybe also because I don't know how to search for it.
I want to import the names.csv file, which holds a bunch of names in the structure: ID, Name, Gender, Year, State, Count
Now I'm trying to transcribe all the names and count
integers into a dictionary which I call names
. I don't understand why it now continually returns me an empty dictionary.
Through the conditionals, I'm trying to say that IF the name is in the dictionary already, it is supposed to sum the count onto the existing count.
Can anyone help? Sadly I'm really a newbie and can't help myself...
with open("../data/names.csv") as file:
names = {}
for lines in file:
data = lines.strip().split(",")
name = data[1]
count = data[5]
if name == "Name":
continue
for name, count in names.items():
if name in names:
names[name] = names[name] + count
else:
names[name] = count
print(names)
Upvotes: 0
Views: 41
Reputation: 64
Better to use defaultdict
from standard python lib (for details link) and for working with csv files csv module (link for details)
from collections import defaultdict
food_list = 'spam spam spam spam spam spam eggs spam'.split()
food_count = defaultdict(int) # default value of int is 0
for food in food_list:
food_count[food] += 1 # increment element's value by 1
food_count
in result you will have:
defaultdict(<type 'int'>, {'eggs': 1, 'spam': 7})
Upvotes: 1
Reputation: 6863
First, you should avoid parsing the CSV file yourself, it can become quite tricky when e.g. quotes are involved. There is a csv module built-in
For your case, I would use pandas. The .groupby()
function together with .sum()
will do exactly what you want:
import pandas as pd
df = pd.read_csv('names.csv')
print(df[['Name', 'Count']].groupby('Name').sum())
Upvotes: 1