I've got a csv with 10+ columns and it's grouped based on an index number. For example,
index othercolumn othercolumn2 sample hits othercolumn3
1 cccc bbbb dog 4 aaaa
1 cccc bbbb cat 1 aaaa
1 cccc bbbb cat 2 aaaa
2 cccc bbbb rat 1 aaaa
2 cccc bbbb dog 1 aaaa
3 cccc bbbb bird 1 aaaa
3 cccc bbbb rat 42 aaaa
3 cccc bbbb cat 3 aaaa
Is it possible to find the maximum number of hits for each "group" (by index)? I'm not too sure what to do in a situation where there is no highest hit, like sample 2, but that's not too important for now. For example, the desired output would be something like,
For index 1, the highest hits are 4 for sample dog.
For index 2, the highest hits are 1 for sample rat.
For index 3, the highest hits are 42 for sample rat.
So far, I've used defaultdict to create a dictionary of lists for each group, or index. But I can't seem to get the highest number of hits and cleanly print that out. So far, this is what I have.
from collections import defaultdict
import csv
groups = defaultdict(list)
with open('data.csv') as inputfile:
reader = csv.reader(inputfile)
next(reader, None) # skip the header row
for row in reader:
groups[row[1]].append([row[17], row[18]]) #row 1 is index, row 17 is my sample column, 18 is the hits column
print(groups)
Would appreciate any help!!