I have over 20 fields and I wish to set the NULL values in the final eight fields to 0. I want to be able to do this both with the field calculator and with the PyQGIS console. At the moment neither are working.
This image shows the NULL values which I wish to set to 0.
When I use the field calculator for one individual column, I am entering the following:
i.e. I enter the code:
if("Animal_2013" is NULL , 0, "Animal_2013" )
in the 'Preview' you can see that it is showing 0, and I have checked that it does show 0 and the non-null values for the correct rows. However, when I press enter nothing changes, but an option to 'update all' appears in the top right. If i click this following message is shown:
'An error occurred while evaluating the calculation string: No root node! Parsing failed?'
As shown below:
How can I solve this?
Additionally, if I am trying to use the Python console (which I would like to know how to do as I will need to do this for a large amount of other columns for a different dataset) I have tried this:
layer = QgsProject().instance().mapLayersByName('weather_tmax') with edit(layer): for row in layer.getFeatures(): data =row.attributes() print(data) for i in [16,17,18,19,20,21,22,23]: if data[i] == NULL: data[i] = 0 row.setAttributes(data) layer.updateFeature(row)
but nothing changes. I have
[16,17,18,19,20, 21, 22, 23] as this corresponds to the indices of the columns of interest, however, I would like to be able to use the column names rather than working out the column numbers for a more general approach if possible.