As input I have a CSV file with times and a bunch of numbers for each time.
Time,F1,F2,F3
8:11,5,2,4
9:25,9,8,2
9:39,7,3,2
9:53,6,5,1
10:07,4,6,7
10:21,7,3,1
10:35,5,6,7
11:49,1,2,1
12:03,3,3,1
I'd like to output the table for each hour grouped by column Avg and Sum:
Time,SUM F1,SUM F2,SUM F3,AVG F1,AVG F2,AVG F3
8:00,5,2,4,5,2,4
9:00,22,16,5,7.3,5.3,1.6
10:00,16,15,15,5.3,5,5
11:00,1,2,1,1,2,1
12:00,3,3,1,3,3,1
So far I was looking at doing it with a dictionary where hour is a key and value is a list of count and sum, then dividing sum by count to get average. I'm sure there must be cleaner way to do it. Maybe some library can work with this. Any suggestions?
The following should get you started, it uses Python's
csv
module to process the files anditertools.groupby
to group the entries by hour:This would give you an output csv file looking like this:
zip
is used to transpose the column entries.Tested using Python 2.7.9
A pandas solution:
prints:
Save as csv file:
This is the content of
res.csv
: