I have a vector layer in QGIS with various numeric fields and I would like to calculate the percentiles, 90-95-99, for each of the fields. I have tried the GRASS v.univariate algorithm but this only does one percentile at a time. Any suggestions?
Asked
Active
Viewed 1,614 times
1
-
Where do you want these percentiles? If you sort on a feature, then the 90th percentile value will be the value of the attribute for the feature at row 0.9 times the number of features. You could simply read that off - I'm not sure how to get a whole set of attribute values into an array to do it in an expression. Otherwise you might need a python function. – Spacedman Mar 06 '21 at 08:43
1 Answers
3
You can use pyqgis with numpy.percentile:
import numpy as np
#Change these two lines to match your data:
lyr = QgsProject.instance().mapLayersByName('ok_my_riks')[0]
fieldname = 'area'
data =[f[fieldname] for f in lyr.getFeatures()] #List all values in column
for p in [90, 95, 99]:
print("{0} th percentile is: {1}".format(p, np.percentile(data, p)))
BERA
- 72,339
- 13
- 72
- 161
