简体   繁体   English

(Python)使用特定元素填充字典

[英](Python) Filling a dictionary with specific elements

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 我想导入names.csv文件,该文件在结构中包含一堆名称:ID,名称,性别,年份,州,计数

Now I'm trying to transcribe all the names and count integers into a dictionary which I call names . 现在,我试图将所有名称转录,并将整数count到字典中,该字典称为“ 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)

Better to use defaultdict from standard python lib ( for details link ) and for working with csv files csv module ( link for details ) 最好使用标准python lib中的defaultdict有关详细信息的链接 )以及使用csv文件csv模块( 有关详细信息的链接

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})

First, you should avoid parsing the CSV file yourself, it can become quite tricky when eg quotes are involved. 首先,您应该避免自己解析CSV文件,当包含引号时,它会变得非常棘手。 There is a csv module built-in 内置了一个csv模块

For your case, I would use pandas . 对于您的情况,我将使用pandas The .groupby() function together with .sum() will do exactly what you want: .groupby()函数与.sum()一起将完全满足您的要求:

import pandas as pd
df = pd.read_csv('names.csv')
print(df[['Name', 'Count']].groupby('Name').sum())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM