解析CSV文件并聚合值

时间:2012-01-10 07:55:22

标签: python file

我想解析一个CSV文件并聚合这些值。城市行有重复值(样本):

CITY,AMOUNT
London,20
Tokyo,45
London,55
New York,25

解析后的结果应该是:

CITY, AMOUNT
London,75
Tokyo,45
New York,25

我编写了以下代码来提取唯一的城市名称:

def main():
    contrib_data = list(csv.DictReader(open('contributions.csv','rU')))
    combined = []
    for row in contrib_data:
      if row['OFFICE'] not in combined:
        combined.append(row['OFFICE'])

我如何汇总值?

2 个答案:

答案 0 :(得分:7)

在Python 3.2.2中测试:

import csv
from collections import defaultdict
reader = csv.DictReader(open('test.csv', newline=''))
cities = defaultdict(int)
for row in reader:
    cities[row["CITY"]] += int(row["AMOUNT"])

writer = csv.writer(open('out.csv', 'w', newline = ''))
writer.writerow(["CITY", "AMOUNT"])
writer.writerows([city, cities[city]] for city in cities)

结果:

CITY,AMOUNT
New York,25
London,75
Tokyo,45

至于你增加的要求:

import csv
from collections import defaultdict

def default_factory():
    return [0, None, None, 0]

reader = csv.DictReader(open('test.csv', newline=''))
cities = defaultdict(default_factory)
for row in reader:
    amount = int(row["AMOUNT"])
    cities[row["CITY"]][0] += amount
    max = cities[row["CITY"]][1]
    cities[row["CITY"]][1] = amount if max is None else amount if amount > max else max
    min = cities[row["CITY"]][2]
    cities[row["CITY"]][2] = amount if min is None else amount if amount < min else min
    cities[row["CITY"]][3] += 1
for city in cities:
    cities[city][3] = cities[city][0]/cities[city][3] # calculate mean

writer = csv.writer(open('out.csv', 'w', newline = ''))
writer.writerow(["CITY", "AMOUNT", "max", "min", "mean"])
writer.writerows([city] + cities[city] for city in cities)

这会给你

CITY,AMOUNT,max,min,mean
New York,25,25,25,25.0
London,75,55,20,37.5
Tokyo,45,45,45,45.0

请注意,在Python 2下,您需要在顶部添加额外的行from __future__ import division才能获得正确的结果。

答案 1 :(得分:0)

使用值为AMOUNT的dict可能会起作用。类似于以下内容 -

假设您一次只读一行,city表示当前城市,amount表示当前金额 -

main_dict = {}

---for loop here---
if city in main_dict:
    main_dict[city] = main_dict[city] + amount
else:
    main_dict[city] = amount
---end for loop---

在循环结束时,您将在main_dict中拥有汇总值。