I have a vector layer with two columns: Code (string) and Area (float/double). The column Code includes some repeated values. I'd like to evaluate for each unique value of Code the maximum value of the Area values which are associated. Similar questions but with different approaches, not suitable with my need, are at Selecting Maximum value for each unique ID ; How can I create a numeric field (not ObjectID) with a unique number for each record?. I made some trials but they don't work at all...any suggestions?
1ST STEP
First of all, based on other posts in the forum (Maximum value of vector layer field in PyQGIS), I tried to implement a script for just calculating the maximum value of the field "Area". It returns
3071.96126727
values = []
for feat in layer.getFeatures():
attrs = feat.attributes()
values.append(attrs[area_index])
print max(values)
2ND STEP (CODE TRIAL)
Then, I tried to insert the previous script into another one for identifying and iterating on unique values. The problem is that it returns a list of the unique values associated only to the maximum value of the whole field Area i.e.
[u'aa1014551', u'ab1014552', etc...] 3071.96126727
uniquevalues=layer.uniqueValues(code_index,limit=10000)
for uv in uniquevalues:
values = []
for feat in layer.getFeatures():
attrs = feat.attributes()
values.append(attrs[area_index])
maximum=max(values) #it also has such a temporaly stuck here
print uniquevalues, maximum