我有以下部分输入文件(超过500行):
L1, a, b, 10, 20, pass,
L1, c, d, 11, 21, pass,
L1, e, f, 12, 22, pass,
L1, a, b, 13, 23, pass,
L1, e, f, 14, 34, pass,
我想得到重复的平均值,即输出如下:
(其中L1,a,b,11.5 =(10 13)/ 2,21.5 =(20 23)/ 2)
L1, a, b, 11.5, 21.5
L1, c, d, 11, 21
L1, e, f, 13, 28
我目前的初学者python代码如下 – 仍然努力更好地调整它
import csv
from collections import defaultdict
import numpy as np
dd = defaultdict(list)
with open("mean.csv") as input_file:
for row in csv.reader(input_file):
dd[tuple(row[:3])].append(float(row[3]))
dd[tuple(row[:3])].append(float(row[4]))
for k, v, m in dd.iteritems():
if len(v) > 1:
print (' '.join(k), np.mean(v), np.mean(m))
我得到的错误是:
Traceback (most recent call last):
File "average.py", line 11, in <module>
for k, v, m in dd.iteritems():
ValueError: need more than 2 values to unpack
最佳答案 未经测试,但像这样的基础可以适用于其他列…因为这只是一个目前.
import csv
from collections import defaultdict
import numpy as np
dd = defaultdict(list)
with open('in.csv') as fin:
for row in csv.reader(fin):
dd[tuple(row[:3])].append(float(row[3]))
for k, v in dd.iteritems():
if len(v) > 1:
print ' '.join(k), np.mean(v)