Skip to main content
deleted 10 characters in body; edited title
Source Link
Matt
  • 19.4k
  • 4
  • 25
  • 64

how to set Set NULL values to 0 in specific columns using PYQGISPyQGIS

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 PYQGISPyQGIS console. At the moment neither are working.

This image shows the NULL values which I wish to set to 0.   

enter image description here

When I use the field calculator for one individual column, I am entering the following:   

enter image description here i

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 thefollowing message: 'An error occurred while evaluating the calculation string is shown: No root node! Parsing failed?' loads, as

'An error occurred while evaluating the calculation string: No root node! Parsing failed?'

As shown below.:

enter image description here

How can I solve this?

Additionally, if I am trying to use the pythonPython 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')[0]
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.

Many thanks in advance!

how to set NULL values to 0 in specific columns using PYQGIS

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.  enter image description here

When I use the field calculator for one individual column, I am entering the following:  enter image description here 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 the message: 'An error occurred while evaluating the calculation string: No root node! Parsing failed?' loads, as shown below.

enter image description here

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')[0]
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.

Many thanks in advance!

Set NULL values to 0 in specific columns using PyQGIS

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. 

enter image description here

When I use the field calculator for one individual column, I am entering the following: 

enter image description here

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:

enter image description here

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')[0]
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.

Source Link

how to set NULL values to 0 in specific columns using PYQGIS

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. enter image description here

When I use the field calculator for one individual column, I am entering the following: enter image description here 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 the message: 'An error occurred while evaluating the calculation string: No root node! Parsing failed?' loads, as shown below.

enter image description here

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')[0]
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.

Many thanks in advance!