I need to know how to calculate the average / mean of an attribute field in my point shape file. Is this possible to do with the field calculator? I just found the options min or max but not mean. The result should be written in a new attribute field for further data processing. I would like to do this within a model.
Calculate Average or Mean of Attribute Field in QGIS Model
field-calculatorqgisqgis-processing
Related Solutions
I think you should use the Summary Statistics tool to calculate the mean:
Calculates summary statistics for field(s) in a table.
...
The following statistical operations are available with this tool: Sum, Mean, Maximum, Minimum, Range, Standard Deviation, Count, First, and Last. The Median operation is not available.
Your "ecoregion type" field would be used as a case field:
If a Case field is specified, statistics will be calculated separately for each unique attribute value. The Output Table will contain only one record if no Case field is specified. If one is specified, there will be one record for each Case field value.
NULL and None
Fun fact: NULL is not None
With this in mind it's easy to fix the code
@qgsfunction(args=-1, group='Custom')
def mean_col(cols, feature, parent):
vals = [feature[i] for i in cols if feature[i] != NULL]
return sum(vals)/float(len(vals))
Do not write to layers in expressions
Additional advice: never write to layers in expressions. Expressions are often executed in threads and in different contexts and using layer.changeAttributeValue()
and similar functions may result in undefined behavior (read: crash).
Using values instead of attribute names
Another interesting experiment for educational purpose.
Instead of providing column names to the function, you can also provide values.
@qgsfunction(args='auto', group='Custom')
def mean_value(vals, feature, parent):
vals = [val for val in vals if val != NULL]
return sum(vals)/float(len(vals))
And then call it with
mean_value("kk1", "kk2", "kk3", "kk4")
The interesting result is: it will return NULL as soon as at least one column is NULL.
How comes? QGIS assumes the result for an expression function is NULL as soon as at least one parameter is NULL for performance reasons. It's possible to control this behavior, but unfortunately the @qgsfunction
decorator does not support this to date (QGIS 3.2).
To workaround this behavior we can either provide a full-blown QgsExpressionFunction
implementation which is out of scope for this answer.
Or just take a little detour and use an array instead.
@qgsfunction(args='auto', group='Custom')
def mean_value(vals, feature, parent):
vals = [val for val in vals if val != NULL]
return sum(vals)/float(len(vals))
and then call it with
mean_value(array("kk1", "kk2", "kk3", "kk4"))
Best Answer
You could get statistics on that field (Vector > Analysis Tools > Basic statistics) which will tell you the mean, and then create a new attribute and use Field Calculator to fill every record with that value.