0

I am going to standardized a vector layer field in Python console in QGIS, for that I want to get mean value and stdev value of the field.

How do I find mean and stdev in using PyQGIS.

I tried this. It is not working

layer=iface.activeLayer()
idx=layer.fields().indexFromName("mean")
ave = layer.meanValue(idx)
print (ave)
stdDev = layer.stdDevValue(idx)
print (stdDev)
6
  • 1
    See my answer to Summing values in one field using PyQGIS. In your case you'd need to use QgsAggregateCalculator.mean and QgsAggregateCalculator.StDev respectively. If that works for you, I'll close this question as a duplicate. Sep 23, 2021 at 3:49
  • Thank you for the solution. But I am getting this error attributeError: type object 'QgsAggregateCalculator' has no attribute 'mean'
    – Chathu_T
    Sep 23, 2021 at 4:58
  • 1
    Should be QgsAggregateCalculator.Mean (with a capital M).
    – Ben W
    Sep 23, 2021 at 5:04
  • Thank you so much I got it. This is working.
    – Chathu_T
    Sep 23, 2021 at 5:10
  • 2
    @Chathu_T, glad you got it working. FYI all Qgis api classes and methods are documented. It is a very good habit to adopt to consult the documentation e.g. qgis.org/api/…
    – Ben W
    Sep 23, 2021 at 5:12

0