Select to view content in your preferred language

Replace Null values with 0 in many fields

3078
6
Jump to solution
03-02-2016 10:10 AM
JAKERODEL
Deactivated User

Can somebody point out what I am doing wrong here?  I am trying to get a list of fields (PrecipFields) and replace all null values with 0.  When I call out a specific field it works fine.  When I use list of fields it looks as if it runs but non of the values are updated. 

>>> with arcpy.da.UpdateCursor("asciiPoints",["ippt_allsites_spas1325_024_19230927_0700_UTC"]) as cursor:

...     for row in cursor:

...         if row[0] == None:

...             row[0] = 0

...             cursor.updateRow(row)

Works fine...

My field list I want to use...

>>> print PrecipFields

[u'ippt_allsites_spas1325_021_19230927_0400_UTC', u'ippt_allsites_spas1325_022_19230927_0500_UTC', u'ippt_allsites_spas1325_023_19230927_0600_UTC', u'ippt_allsites_spas1325_024_19230927_0700_UTC', u'ippt_allsites_spas1325_025_19230927_0800_UTC', u'ippt_allsites_spas1325_026_19230927_0900_UTC', u'ippt_allsites_spas1325_027_19230927_1000_UTC', u'ippt_allsites_spas1325_028_19230927_1100_UTC'

This is a shortened list.  There could be hundreds of fields.

Nothing happens...

>>> with arcpy.da.UpdateCursor("asciiPoints","PrecipFields") as cursor:

...     for row in cursor:

...         if row[0] == None:

...             row[0] = 0

...             cursor.updateRow(row)

...

0 Kudos
1 Solution

Accepted Solutions
RuthBowers
Deactivated User

Hi, Jake. You have to enumerate through all the fields. As Darren pointed out, row[0] refers to the first column in the row. I pasted in an example below. When you use enumerate, using my example, i is the number of the column position from the left (of the columns included in the cursor), starting at 0. c is the corresponding value from the list being enumerated through, in your case, the column name.

# Assuming PrecipFields is a python list of column names.

with arcpy.da.UpdateCursor("asciiPoints", PrecipFields) as cursor:

    for row in cursor:

        updated = False

        for i, c in enumerate(PrecipFields):

            if row == None:

                row = 0

                updated = True

        if updated == True:

            cursor.updateRow(row)

I hope this is helpful.

View solution in original post

6 Replies
DarrenWiens2
MVP Honored Contributor

Remove the quotes around "PrecipFields". Quotes mean text/string. No quotes means variable, in your case, a list of field names.

0 Kudos
JAKERODEL
Deactivated User

That doesn't work for me either.  No error just nothing happens. 

0 Kudos
DarrenWiens2
MVP Honored Contributor

You'll also have to loop through the fields in your field list. row[0] means look at the first field only.

JAKERODEL
Deactivated User

This would do that right?  Same result...

>>> with arcpy.da.UpdateCursor("asciiPoints",PrecipFields) as cursor:

...     for row in cursor:

...         for field in PrecipFields:

...             if row[0] == None:

...              row[0] = 0  

...              cursor.updateRow(row)

Also tried this...

>>> i = 0

>>> with arcpy.da.UpdateCursor("asciiPoints",PrecipFields) as cursor:

...   for row in cursor: 

...       if row == None:

...          row = 0

...          cursor.updateRow(row)

...          i += 1

0 Kudos
RuthBowers
Deactivated User

Hi, Jake. You have to enumerate through all the fields. As Darren pointed out, row[0] refers to the first column in the row. I pasted in an example below. When you use enumerate, using my example, i is the number of the column position from the left (of the columns included in the cursor), starting at 0. c is the corresponding value from the list being enumerated through, in your case, the column name.

# Assuming PrecipFields is a python list of column names.

with arcpy.da.UpdateCursor("asciiPoints", PrecipFields) as cursor:

    for row in cursor:

        updated = False

        for i, c in enumerate(PrecipFields):

            if row == None:

                row = 0

                updated = True

        if updated == True:

            cursor.updateRow(row)

I hope this is helpful.

JAKERODEL
Deactivated User

Perfect.  Thanks to both of you!

0 Kudos