简体   繁体   English

Python:如何在CSV文件中求和,而仅求和某个变量的整数?

[英]Python: How can I sum integers in a CSV file, while only summing the integers of a certain variable?

I'm trying to program some data in a csvfile by using Python. 我正在尝试使用Python在csvfile中编程一些数据。 I have a list of countries and results of the Eurovision Songcontest, and it looks like this: 我有一个国家列表和欧洲歌唱大赛的结果,它看起来像这样:

Country,Points,Year
Belgium;181;2016
Netherlands;153;2016
Australia;511;2016
Belgium;217;2015
Australia;196;2015

Et cetera. 等等。

In summary, I want to sum the total of points that any country received throughout the years, so the output should look something like this: 'Belgium: 398','Netherlands: 153','Australia: 707' and so on. 总而言之,我想对所有国家多年来获得的总积分进行汇总,因此输出应如下所示:“比利时:398”,“荷兰:153”,“澳大利亚:707”,依此类推。

This is what my code looks like: 这是我的代码如下所示:

import csv
with open('euro20042016.csv', 'r') as csvfile:
    pointsallyears = []
    countriesallyears = []
    readFILE = csv.reader(csvfile, delimiter=';')
    for row in readFILE:
        countriesallyears.append(row[0])
        pointsallyears.append(row[1])
csvfile.close()

results = []
for result in pointsallyears:
    result = int(result)
    results.append(result)

scorebord = zip(countriesallyears,results)

So I already made sure that the results / points are actual integers and I filtered out the third row (Year), but I have no idea how to proceed from here. 所以我已经确保结果/点是实际的整数,并且我过滤掉了第三行(年份),但是我不知道如何从这里继续。 Thanks a lot in advance! 在此先多谢!

Just put @Mikk's comment into an actual answer. 只需将@Mikk的评论放入实际答案中即可。 Two lines except the import import外的两行

import pandas as pd
df = pd.read_csv('euro20042016.csv', sep = ';')
print df.groupby('Country')['Points'].sum()

The only extra thing you need to do is to change the first line of your file to be delimited by ; 您唯一需要做的额外事情就是更改文件的第一行,以第一行分隔; instead of , . 而不是,

I slightly changed your code to use a dictionary and used country names as keys. 我稍微更改了您的代码以使用字典,并使用国家/地区名称作为键。 In result dictionary d will have country names as key and value is the total points. 结果字典d将以国家/地区名称作为关键字,值是总分。

import csv

d = dict()

with open('euro20042016.csv', 'r') as csvfile:
    readFILE = csv.reader(csvfile, delimiter=';')
    print (readFILE)
    c_list = []
    for row in readFILE:
        if row[0] in c_list:
            d[row[0]] = d[row[0]] + int(row[1])
        else:
            c_list.append(row[0])
            d[row[0]] = int(row[1])
csvfile.close()

print(d)

I decided to play around a bit with your code, and this is what I came up with. 我决定花点时间处理您的代码,这就是我想到的。 Here, row[0] contains the country names, and row[1] contains the values we need. 在这里, row[0]包含国家/地区名称, row[1]包含我们所需的值。 We check if the country already exists in the dictionary we use to maintain the aggregates, and if it doesn't we create it. 我们检查用于维护聚合的词典中是否已经存在该国家,如果不存在,我们将创建该国家。

import csv
with open('euro20042016.csv', 'r') as csvfile:
score_dict={}
readFILE = csv.reader(csvfile, delimiter=';')
for row in readFILE:
    # Only rows with 3 elements have the data we need
    if len(row) == 3:
        if row[0] in score_dict:
            score_dict[row[0]]+=int(row[1])
        else:
            score_dict[row[0]]=int(row[1])
csvfile.close()
print score_dict

What I get as output is this 我得到的输出是这个

{'Belgium': 398, 'Australia': 707, 'Netherlands': 153}

which I believe is what you were aiming for. 我相信这是您的目标。

Let me know in the comments if you face a problem understanding anything. 如果您在理解任何内容时遇到问题,请在评论中让我知道。

I have solution of that. 我有解决方案。 but make sure your euro20042016.csv file same as 但请确保您的euro20042016.csv文件与

Belgium;181;2016
Netherlands;153;2016
Australia;511;2016
Belgium;217;2015
Australia;196;2015

and this code get output in list. 然后此代码将输出到列表中。 like 喜欢

[('Belgium', 398), ('Australia', 707), ('Netherlands', 153)]

Code is here 代码在这里

try:
    f = open('euro20042016.csv', 'r+')
    s = f.read()

    lst = list(map(lambda x: x.split(';'), s.split('\n')))

    points, country = [], []
    for line in lst:
        points.append(int(line[1]))
        country.append(line[0])

    countrypoints = sorted(zip(country, points), key=lambda x: x[1])
    country = list(set(country))
    total = [0]*len(country)

    for rec in countrypoints:
        total[country.index(rec[0])] = total[country.index(
            rec[0])] + rec[1]
    f.close()
    finalTotal = list(zip(country, total))
    print finalTotal

except IOError as ex:
    print ex
except Exception as ex:
    print ex

I hope this will help you. 我希望这能帮到您。

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

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