3

I am using Python 2.7 and am trying to generate a list of unique values from all values within two fields (cropsum and cropsum2). I am not trying to find unique row combinations. I've tried using both methods from http://geospatialtraining.com/get-a-list-of-unique-attribute-values-using-arcpy/ but neither method is working for me using two fields. My preference is to use numpy as and I am trying to write a script to manipulate data from a Frequency tool dbf output.

Method 1: Search Cursor

in_table = 'frequencySoutheastSubset.dbf'
field_names = ['cropsum', 'cropsum2']
with arcpy.da.SearchCursor(in_table, [field_names]) as cursor:
    uniqueCropCodes = sorted({row[0] for row in cursor})                   
    print uniqueCropCodes

returns the following error: TypeError: 'field_names' must be string or non empty sequence of strings

Method 2: Numpy

arr = arcpy.da.TableToNumPyArray('frequencyNorthSubset.dbf',['cropsum', 'cropsum2'])
print(arr)

uniqueCropCodes = numpy.unique(arr)
print uniqueCropCodes

Returns unique row combinations, which is not what I want. The numpy.unique documentation example

>>> a = np.array([[1, 1], [2, 3]])
>>> np.unique(a)
array([1, 2, 3])

suggests that the code above should work.

I tried suggestions from this np.unique(arr[['cropsum', 'cropsum2']].values) did not work either: AttributeError: 'numpy.ndarray' object has no attribute 'values'

My dbf in case it's helpful:enter image description here

2
  • Remove the brackets around field_names in the cursor
    – BERA
    Mar 9, 2018 at 18:48
  • @BERA Thanks, I just tried that and it only gives a list from the first column, cropsum [0, 1, 14, 31, 81, 98, 9811, 9817]. It doesn't add unique value 51 from cropsum2...
    – ENIAC-6
    Mar 9, 2018 at 18:54

1 Answer 1

3

Use a set:

A set is an unordered collection with no duplicate elements

import arcpy
in_table = 'frequencySoutheastSubset.dbf'
field_names = ['cropsum', 'cropsum2']

all_values = []

with arcpy.da.SearchCursor(in_table,field_names) as cursor:
    for row in cursor:
        all_values.extend(list(row))

unique_values = set(all_values)

print unique_values

This should also work:

from itertools import chain

unique_values = set(list(chain(*[i for i in arcpy.da.SearchCursor(in_table, field_names)]))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.