Reputation: 13
I have a csv file with several thousand rows that I am trying to parse. 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.
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?
Upvotes: 0
Views: 236
Reputation: 113958
a csv.reader
is merely an iterator over the rows that returns each row as a list
you can convert any iterator into a sequence with 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)
Upvotes: 1