3

我有以下部分输入文件(超过 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
4

2 回答 2

6

未经测试,但像这样作为基础的东西可以适用于另一列......因为这只是目前的一个。

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)
于 2012-07-24T23:15:41.857 回答
1

这样会很短(pandas而且应该很快)。

你可以做这样的事情(不知道你的列的含义或命名,所以这取决于你想用什么作为你的索引DataFrame):

In [1]: df = pd.read_csv('mean.csv', delimiter=',', header=None)

In [2]: df
Out[2]: 
  X.1 X.2 X.3  X.4  X.5
0  L1   a   b   10   20
1  L1   c   d   11   21
2  L1   e   f   12   22
3  L1   a   b   13   23
4  L1   e   f   14   34

In [3]: df.groupby(['X.1', 'X.2', 'X.3']).mean()
Out[3]: 
              X.4   X.5
X.1 X.2 X.3            
L1   a   b   11.5  21.5
     c   d   11.0  21.0
     e   f   13.0  28.0
于 2012-07-25T20:00:59.683 回答