简体   繁体   English

Python csv按n行号分组行

[英]Python csv group rows by n row numbers

I have a csv file with several thousand rows that I am trying to parse. 我有一个要解析的数千行的csv文件。 I want to group the rows into sets of 5 then calculate the average of the 'Value' column as well as return the min and max 'Value' of that group and the End Time of when that min and max value occurred. 我想将行分组为5组,然后计算“值”列的平均值,并返回该组的最小和最大“值”以及该最小和最大值出现的结束时间。

Start Time,End Time,Value
12-4-2014 9:00,12-4-2014 10:00,3221.3
12-4-2014 10:00,12-4-2014 11:00,3233.5
12-4-2014 11:00,12-4-2014 12:00,3543.6
12-4-2014 12:00,12-4-2014 13:00,3711.5
12-4-2014 13:00,12-4-2014 14:00,3732.4
etc....

I am thinking I have to create a dictionary for each set of 5 then run some stats on that dictionary? 我想我必须为每5个集合创建一个字典,然后在该字典上运行一些统计信息?

a csv.reader is merely an iterator over the rows that returns each row as a list csv.reader仅仅是行上的迭代器,它将每一行作为列表返回

you can convert any iterator into a sequence with list(iterator) 您可以使用list(iterator)将任何迭代器转换为序列

you can group any sequence as follows 您可以将任何序列分组如下

step = 5
split_data = [data[i:i+step] for i in range(0,len(data),step)]

you can then iterate over each group 然后您可以遍历每个组

split_data = (data[i:i+step] for i in range(0,len(data),step))
#note i use an iterator comprehension this time since i dont want to loop over the stuff twice

for grouping in split_data:
    analyze(grouping)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM