I would like to write a Python expression function which calculates the average of columns (variable length parameters), skipping NULL values.
@qgsfunction(args=-1, group='Custom')
def col_avg(vals, feature, parent):
v = [ x for x in vals if x is not NULL]
if len(v):
return sum(v) / len(v)
return 0
If there is a NULL value among the input parameters the result is NULL, too. If I use an expression in Select by Expression for example:
my_col is not NULL
it works. I use QGIS 2.18.7 on Ubuntu 16.04
* addition * Here is an image, how I call my function:
No comments:
Post a Comment