I have no experience with Python, but in pseudocode this is how I would do it:
float averageIf(list[][], uniqueCode)
{
sum=0, count=0; //sum is where you will store all the values you are averaging and count keeps count so you know what to divide by
for i = 1 to list.size() //iterate through the list
if list[0]==uniqueCode then //if this row contains the unique code then you can ->
sum+=list[1]; //add the value in the second column to the sum and ->
count++; //increment the counter
return sum/count; //once you have exhausted the list, return the average
}
Wont matter unless OP is doing tens of thousands of rows but in general this wouldn't be very efficient bc you'd have iterate through the list for each unique code. More efficient way would be to iterate through it once and then keep track total and count for each unique code.
haven't actually tried this but something similar to below should work. You could definitely do it in less code but would be harder to read/understand.
Alternative would be dump your rows into a simple sql table (in memory sqllite would be 1 or 2 lines of code) and then just query it easily with something like " select uniqueCode, avg(c1), avg(c2),avg(c3),avg(c4) from rows group by uniqueCode" and that would do all the math for you.
#get rows from somwhere
rows = getInput()
#calculate totals
numTimesUniqueCodeFound = defaultdict(int) #this will create dictionary with default of 0 for new keys
totalsForEachUniqueCode = defaultdict(list)#this will create dictionary with default of empty list for new keys
for row in rows:
uniqueCode = row[0] #or whatever column unique code is in
numTimesUniqueCodeFound[uniqueCode] += 1
for cColumnNumber in range(1, 10):
totalsForEachUniqueCode[uniqueCode][cColumnNumber] += row[cColumnNumber]
#now get averages
averageForEachCode = {}
for uniqueCode in totalsForEachUniqueCode:
averageForEachCode[uniqueCode] = []
for cColumnNumber in range(1, 10):
averageForEachCode[uniqueCode][cColumnNumber] = totalsForEachUniqueCode[uniqueCode][cColumnNumber] / numTimesUniqueCodeFound[uniqueCode]
#now do something with averages
for code in averageForEachCode.keys():
print "{},".format(code)
for cColumnNumber in range(1, 10):
print "{},".format(averageForEachCode[code][cColumnNumber])