简体   繁体   English

使用python,我需要根据CSV文件中两列中的两个键取平均值

[英]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.

相关问题 我需要使用 python 将 csv 文件中的一列拆分为两列 - I need to split one column in csv file into two columns using python 根据第三列中的值使用python比较CSV文件中的两列 - Comparing two columns in a csv file using python based on value in third column 使用 Pandas 如何根据来自不同 csv 文件的两列复制行值 - Using Pandas how to copy row value based on two Columns from different csv file 根据两列匹配来自两个csv文件的数据,并使用选定的列创建一个新的csv文件 - Matching data from two csv files based on two columns and creating a new csv file with selected columns 如何根据来自其他两列的两组描述符从 CSV 文件的列中打印 Python 3.9 中的一组特定数据? - How can I print a specific set of data in Python 3.9, from a column of a CSV file based on two sets of descriptors from two other columns? 根据用户输入从 csv 文件的两列中检索数据 - Retrieve data from two columns of csv file based on user input 如何仅排序和存储两个.CSV 文件中的前 3 个位置,然后使用 Python 将它们存储到一个.CSV 文件中的两列? - How to sort and store only the top 3 locations from two .CSV files and then store them into two columns in one .CSV file using Python? 如何使用python将文本文件中的两列提取到csv文件中? - How to extract two columns from a text file into a csv file using python? 基于python中的两列映射或合并python中的两个csv文件? - Mapping or Merging two csv files in python based on two columns in python? 如何使用python在CSV文件中插入两列? - How to insert two columns in a CSV File using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM