在csv文件python中通过另一列获取一列的值
我的csv文件如下所示:在csv文件python中通过另一列获取一列的值,python,Python,我的csv文件如下所示: ID Value Amount ---- ------- ------- A 3 2 A 4 4 B 3 6 C 5 5 A 3 2 B 10 1 我想要“Value”或“Amount”列与“ID”列之和
ID Value Amount
---- ------- -------
A 3 2
A 4 4
B 3 6
C 5 5
A 3 2
B 10 1
我想要“Value”或“Amount”列与“ID”列之和。我想要的输出是,对于“A”,它应该给我所有与平均值[3+4+3]相关的值的总和
我的代码:
import csv
file = open(datafile.csv)
rows=csv.DictReader(file)
summ=0.0
count=0
for r in rows:
summ=summ+int(r['Value'])
count=count+1
print "Mean for column Value is: ",(summ/count)
file.close()
您可以使用
list
的defaultdict
按ID列对数据进行分组。然后使用sum()
生成总计
from collections import defaultdict
with open('datafile.csv') as f:
d = defaultdict(list)
next(f) # skip first header line
next(f) # skip second header line
for line in f:
id_, value, amount = line.split()
d[id_].append((int(value), int(amount)))
# sum and average of column Value by ID
for id_ in d:
total = sum(t[0] for t in d[id_])
average = total / float(len(d[id_]))
print('{}: sum = {}, avg = {:.2f}'.format(id_, total, average))
输入数据的输出:
A: sum = 10, avg = 3.33
C: sum = 5, avg = 5.00
B: sum = 13, avg = 6.50
我不想使用收藏。@mohammadshahbazKhan:真的吗?为什么不呢?我正在清理python的基本技能。我试图找出不使用集合的方法。字典算“集合”吗?名单呢?如果没有诸如数据结构之类的集合,要做到这一点非常困难。您可以使用字典或列表。
with open('datafile.csv') as f:
d = {}
next(f) # skip first header line
next(f) # skip second header line
for line in f:
id_, value, amount = line.split()
d[id_] = d.get(id_, []) + [(int(value), int(amount))]
# sum and average of column Value by ID
for id_ in d:
total = sum(t[0] for t in d[id_])
average = total / float(len(d[id_]))
print('{}: sum = {}, avg = {:.2f}'.format(id_, total, average))