Hi XanderThe following is exactly what I'm trying to achieve. I've not worked with python dictionaries yet. Would you mind explaining to me how to convert the python dictionary into a File Geodatabase Table?Thanks for all your help so far, its much appreciated.Regards
import arcpy, os, numpy arcpy.env.overwriteOutput = True # your data: tbl = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb\aTable' fld_quin = 'Quinary' fld_wma = 'WMA' # Water Management Area fld_perc = 'Percentage' fld_oid = arcpy.Describe(tbl).OIDfieldname # or specify ID fieldname # extra vars for output fld_id = 'ID' out_ws = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb' tbl_name = 'aNewOutputTable' # create empty dictionary to store results dct = {} # loop through table flds = (fld_oid, fld_quin, fld_wma, fld_perc) with arcpy.da.SearchCursor(tbl, flds) as curs: for row in curs: oid, perc = row[0], row[3] quin, wma = row[1], row[2] if quin in dct: max_perc = dct[quin][2] if perc > max_perc: dct[quin] = (oid, wma, perc) else: dct[quin] = (oid, wma, perc) # create list for use in numpy lst_out = [(val[0], quin, val[1], val[2]) for quin, val in dct.items()] # create a numpy array npa = numpy.array(lst_out, numpy.dtype([(fld_id, numpy.int32), (fld_quin, '|S8'), (fld_wma, '|S8'), (fld_perc, numpy.float)])) # output list of fields flds = (fld_id, fld_quin, fld_wma, fld_perc) # store the table arcpy.da.NumPyArrayToTable(npa, os.path.join(out_ws, tbl_name), flds)
import pandas as pd import numpy as np #just pull in sample data into a gbd table _dfsource = r'H:\Documents\ArcGIS\Default.gdb\sampdat' #specify fields to use and convert the gdb tab to a numpy array flds = ['ID', 'Field01', 'Field02', 'Field03'] _numparr = arcpy.da.TableToNumPyArray(_dfsource, flds) #convert the numpy array to a pandas data frame for easy-peezy grouping functions _dfarr = pd.DataFrame(_numparr, columns=['ID', 'Field01', 'Field02', 'Field03']) _dfarrGrouped = _dfarr.groupby(['Field01'])['Field03'].max().reset_index() print _dfarrGrouped
import arcpy # your data: tbl = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb\aTable' fld_quin = 'Quinary' fld_wma = 'WMA' # Water Management Area fld_perc = 'Percentage' fld_oid = arcpy.Describe(tbl).OIDfieldname # or specify ID fieldname # create empty dictionary to store results dct = {} # loop through table flds = (fld_oid, fld_quin, fld_wma, fld_perc) with arcpy.da.SearchCursor(tbl, flds) as curs: for row in curs: oid, perc = row[0], row[3] quin, wma = row[1], row[2] if quin in dct: max_perc = dct[quin][2] if perc > max_perc: dct[quin] = (oid, wma, perc) else: dct[quin] = (oid, wma, perc) # print results for quin, val in dct.items(): print val[0], quin, val[1], val[2]
import arcpy # your data: tbl = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb\aTable' fld_quin = 'Quinary' fld_wma = 'WMA' # Water Management Area fld_perc = 'Percentage' fld_oid = arcpy.Describe(tbl).OIDfieldname # create empty dictionary to store results dct = {} # loop through table flds = (fld_oid, fld_quin, fld_wma, fld_perc) with arcpy.da.SearchCursor(tbl, flds) as curs: for row in curs: oid, perc = row[0], row[3] quin, wma = row[1], row[2] key = (quin, wma) if key in dct: max_perc = dct[key][1] if perc > max_perc: dct[key] = (oid, perc) else: dct[key] = (oid, perc) # print results for key, val in dct.items(): print val[0], key[0], key[1], val[1]
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.