[英]Using python, I need to average value based on two keys in two columns from a CSV file
I have a csv file with 3 columns 我有3列的csv文件
TMC, EPOCH, Time
11C12, 1, 24
11C12, 1, 34
11C12, 2, 56
11C12, 2, 78
11C13, 1, 56
11C13, 2, 45
11C13, 2, 64
11C13, 3, 32
11C13, 3, 28
Now I want to have average.py file which calculates average of time for each combination of TMC, EPOCH and write that to a txt or csv file 现在,我想拥有一个average.py文件,该文件可以计算TMC,EPOCH每种组合的平均时间,并将其写入txt或csv文件
The desired output is: 所需的输出是:
TMC, EPOCH, Average Time
11C12, 1, average value
11C12, 2, average value
11C13, 1, average value
11C13, 2, average value
11C13, 3, average value
Use a defaultdict
to group the elements using the forst two columns as the key and appending the times then average and write to the new csv: 使用
defaultdict
使用前两列作为关键字对元素进行分组,然后附加时间,然后取平均值并写入新的csv:
import csv
from collections import defaultdict
with open("in.csv") as f, open("average.csv", "w") as out:
wr = csv.writer(out)
d = defaultdict(list)
head = next(f)
out.write(head)
for row in csv.reader(f):
d[tuple(row[:2])].append(int(row[-1]))
for k, v in d.items():
out.write("{},{},{}\n".format(k[0], k[1], sum(v, 0.0) / len(v)))
Output: 输出:
TMC,EPOCH,Time
11C12,1,29.0
11C12,2,67.0
11C13,1,56.0
11C13,2,54.5
11C13,3,30.0
If you want to keep the order the elements are first seen you can use an OrderedDict
: 如果要保持顺序,首先可以看到元素,则可以使用
OrderedDict
:
import csv
from collections import OrderedDict
with open("in.csv") as f, open("average.csv", "w") as out:
wr = csv.writer(out)
d = OrderedDict()
head = next(f)
out.write(head)
for row in csv.reader(f):
d.setdefault(tuple(row[:2]), []).append(int(row[-1]))
for k, v in d.items():
out.write("{},{},{}\n".format(k[0], k[1], sum(v, 0.0) / len(v)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.