Select to view content in your preferred language

Split by Attribute

3023
15
Jump to solution
05-01-2013 10:19 AM
JohnDye
Deactivated User
I have a Feature Class with 3.2 million records which represents customers. Each customer is assigned to a particular store ID (DID)

So I'm trying to split the customer record into store ID datasets. That is, for each store ID (DID), create a dataset of the customers assigned to it, essentially splitting the FC by each unique attribute in the field.

I was able to pull up a list of the unique store IDs by using:

def Unique_Values(table, field):     with arcpy.da.SearchCursor(table, [field]) as cursor:         return sorted({row[0] for row in cursor})  fc = "C:\Users\username\Documents\ArcGIS\Default.gdb\MyCustomers" field = "DID"  UniqueValues (fc, field)


which resulted in a list of all of my unique store IDs, 957 in all.

What I'm not sure of however, is how to go about iterating through that list and using each value in the list in a where_clause in a "FeatureClassToFeatureClass_conversion" to export each unique dataset.

I know there are third party developed tools to do this, but they don't seem to work due to the number of records. I've tried every single one I could find.
Tags (2)
0 Kudos
15 Replies
MathewCoyle
Honored Contributor
Have you tried this tool?

http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=37AEB018-1422-2418-A036-CA6D9...

And I imagine the problem with your query is the quotes. Try this using string substitution instead.

"CrimeType = '{0}'".format(CrimeType)
0 Kudos
AmberPerenzin
Emerging Contributor
Have you tried this tool?

http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=37AEB018-1422-2418-A036-CA6D9...


I have--but I need to use the Python script because this is just one of many steps I am programming 😕 the python script for that specific tool is VERY long and complicated.  I am new to python, so the script that Chris posted is perfect for what I am doing.  I just cannot format the expression correctly.  Python is so frustrating!

The string substitution isn't working for me, but I could be entering it wrong.  I will keep trying 🙂 thank you for the suggestion!
0 Kudos
ChrisSnyder
Honored Contributor
Since "crime type" is probably a string field, you just need single quotes.

So instead of this:

storeIdSet = set([r[0] for r in arcpy.da.SearchCursor(myFC, ["STORE_ID"])])
for storeId in storeIdSet:
     outFC = r"C:\temp\test.gdb\store_" + str(storeId)
     arcpy.Select_analysis(myFC, outFC, "STORE_ID = " + str(storeId))


Do this (difference is in the last line):

storeIdSet = set([r[0] for r in arcpy.da.SearchCursor(myFC, ["STORE_ID"])])
for storeId in storeIdSet:
     outFC = r"C:\temp\test.gdb\store_" + str(storeId)
     arcpy.Select_analysis(myFC, outFC, "STORE_ID = '" + str(storeId) +"'")


Mzcoyle formatting stuff also will work... Basically I think you just need to edit the SQL expression so it will deal with string values instead of numbers...
0 Kudos
JamesCrandall
MVP Alum
Make sure "CrimeType" is a field that actually exists in CopyCopySample2011_2013.shp.

Also: "CopyCopy"????
0 Kudos
MathewCoyle
Honored Contributor
It could also be you need delimiters on your field, though it should be unnecessary.
AllCrimes = "CopyCopySample2011_2013.shp"
CrimeTypeSet = set([r[0] for r in arcpy.da.SearchCursor(AllCrimes, ["CrimeType"])])
for CrimeType in CrimeTypeSet:
    out_FC = "H:\\PythonOutput\\" + str(CrimeType)
    query = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(AllCrimes, "CrimeType"), CrimeType)
    arcpy.Select_analysis(AllCrimes, out_FC, query)
0 Kudos
AmberPerenzin
Emerging Contributor
SUCCESS!!

Thank you both!  When I entered the single quotes everything worked!

mzcoyle: ref "copy copy" Since I am new to python I know I am going to screw up my dataset.  Making one backup wasn't enough, I had to backup the backup 🙂

Thank you again! I was stuck on this for longer than I would like to admit (days, not hours...ugh).  You are a lifesaver!
0 Kudos