1

I need to sum values of 2 columns in field calculator and populate a new column with the result, one of the values always in Null. I already had found a useful answer In here, but it doesn't help with the last couple of columns, that contain a date in DD.MM.YYYY (string) format- geoprocessing report shows successful operation, but the new column stays empty. Is there a small tweak i could use for the code, to make it work?

def stack(Y, Y_1): itemList = [Y, Y_1] myList = [Y for Y in itemList if (Y != None)] return sum(myList)

Field value= stack( !Y!, !Y_1!)

Data table

User6819
  • 69
  • 9
  • Can you add your code? – MacroZED Sep 30 '16 at 10:31
  • Added the code to the question. – User6819 Sep 30 '16 at 10:35
  • Have you tried converting the Nulls to 0 and then trying to calculate the fields? Try if Y is None, return 0. Then go ahead and try your calculation again – MacroZED Sep 30 '16 at 10:39
  • Can you also show the table with the fields and what type of data it has? – MacroZED Sep 30 '16 at 10:40
  • I haven't tried converting Nulls to 0, but the code worked well with Nulls, when calculating double format data, these are string. – User6819 Sep 30 '16 at 10:46
  • Are you trying to add two strings together? Need to know what youre trying to accomplish? If they are dates, in a date format, this can be done with some python – MacroZED Sep 30 '16 at 10:48
  • Basically i want to add the two columns together, because one of them always has Null, the other a value (approximately half of the valuable data stored in each column). They are stored in string and should stay string after calculation, too. – User6819 Sep 30 '16 at 10:55
  • so some data is in one field, and some in another. What happens if there is data in both columns for a row? – MacroZED Sep 30 '16 at 10:57
  • As i said before, one of the columns always has data, the other always is Null. – User6819 Sep 30 '16 at 11:00

2 Answers2

2

Based on your comments, it sounds like sum is not quite the right name for the field. You want to coalesce two fields.

Field value = !Y! or !Y_1!

This post on StackOverflow can give you more background on coalesce.

Richard Morgan
  • 2,819
  • 1
  • 21
  • 28
0

Try the below. It looks into the first column (date) and if there nothing in it, will return that into the sum field. And vice versa for the date_1 field.

def stack (date, date_1, date_sum):
    if date is None:
        return date_1
    else:
        return date

date_sum = stack (!date!, !date_1!, !date_sum!)
MacroZED
  • 2,291
  • 1
  • 12
  • 22