Select to view content in your preferred language

arcpy function for attributes in a field?

7409
10
Jump to solution
05-17-2015 12:28 PM
BradJones
Deactivated User

Is there a function in arcpy that will list all the different individual attributes in a field? I know I have seen this somewhere but can't remember, or find it.  I want to create a script to export data in a fc based on attributes.

0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor

Several approaches work.  ModelBuilder does this with a Iterate Field Values iterator with the Unique Value option checked.  The iterator provides the current value being processed in a variable that can be used to name files using Feature Class to Feature Class for an export.  All of the steps can be done through a model without any Python scripting.

Summary Statistics works also, because it exactly provides a list of strings from a source text field into a new text field with only the sorted, unique values listed if you use that field as the Case field as Owen suggested.  This can be output to an in memory output in a script if you just want to read the values for naming files.   A second for loop with a cursor would read the list and use a tool like Feature Class to Feature Class to export each file and do the naming.

A cursor can do this also, but it is not an absolute requirement to take that approach.  It is done with two for loops.  A search cursor for loop would read the values into a list which could be sorted with the sort function and then made unique using the set function, then a second for loop would read the list and use a tool like Feature Class to Feature Class to export each file and do the naming.

If the end goal is simply to create a table of unique values in a field all you need is the Summary Statistics tool as everyone else has said.

If the goal is to create a single feature based on each of the unique attributes in a field use Dissolve with the Case Field assigned and the create multi-part features option checked.

View solution in original post

10 Replies
DanPatterson_Retired
MVP Emeritus

are you referring to either Summary Statistics or the Split Tool​

BradJones
Deactivated User

​Neither I believe. If I'm not mistaken the summary statistics tool will not provide a list of atributes (strings) in a field and e split tool requires a second feature to base the split on, as well as a field. Am I wrong?

0 Kudos
RichardFairhurst
MVP Honored Contributor

Several approaches work.  ModelBuilder does this with a Iterate Field Values iterator with the Unique Value option checked.  The iterator provides the current value being processed in a variable that can be used to name files using Feature Class to Feature Class for an export.  All of the steps can be done through a model without any Python scripting.

Summary Statistics works also, because it exactly provides a list of strings from a source text field into a new text field with only the sorted, unique values listed if you use that field as the Case field as Owen suggested.  This can be output to an in memory output in a script if you just want to read the values for naming files.   A second for loop with a cursor would read the list and use a tool like Feature Class to Feature Class to export each file and do the naming.

A cursor can do this also, but it is not an absolute requirement to take that approach.  It is done with two for loops.  A search cursor for loop would read the values into a list which could be sorted with the sort function and then made unique using the set function, then a second for loop would read the list and use a tool like Feature Class to Feature Class to export each file and do the naming.

If the end goal is simply to create a table of unique values in a field all you need is the Summary Statistics tool as everyone else has said.

If the goal is to create a single feature based on each of the unique attributes in a field use Dissolve with the Case Field assigned and the create multi-part features option checked.

OwenEarley
Frequent Contributor

Summary Statistics would be the starting point for your script:

arcpy.Statistics_analysis("DevTypes","C:/Users/Owen/Documents/ArcGIS/Default.gdb/DevTypes_Stats","OBJECTID FIRST","ClassCode")

If you are only interested in the unique values in a field then use it as the Case Field parameter. For the Statistics Field(s) parameter you can just use the first ObjectId (the output for this is not used but it is a required input). You will end up with something like:

summary-stats.png

Then you can use a search cursor to loop through the field values for the export stage.

BradJones
Deactivated User

​A coworker asked me about this ( he doesn't know Python) and I said it would require a python script with a search curso. But I remember being shown a way to produce a list of attributes from a field. I thought it was an arcpy function. Then a "for" loop could be used to loop through the attributes for the export. Just a thought.

0 Kudos
DanPatterson_Retired
MVP Emeritus

summary statistics ... the tool in arctoolbox, will give you what you need, or you can extract the necessary code from my Field Statistics Toolbox..

curtvprice
MVP Esteemed Contributor

Here's how to do it a search cursor (very quickly, compared to the Frequency or Statistics tools) -- if what you want is Python list.

def ListUnique(tbl, field):
    vlist = []
    with arcpy.da.SearchCursor(tbl, field) as rows:
        for row in rows:
            vlist.append(row[0])
    return sorted(list(set(vlist))) # unique-ize and sort the list

Note that you don't need to sort the list if you don't need to:

>>> list(set([3,2,1,4,3,99, 98]))
[1, 2, 99, 4, 98, 3]

([LISTUNIQUE] was a very often-used function back in AML pre-history. Those were the days, my friends...)

BradJones
Deactivated User

Thanks!

0 Kudos
BradJones
Deactivated User

​Thanks guys. You all were a big help.

0 Kudos