1

I have a table and I want to find max and min value in specified field (f1) with their names. I used this code but I only could print max and min values in f1. How can I print their names Like the image I put I want to print min value of f1 and its name net72.shp. I think it must be using WHERE_CLAUSE.

table

import arcpy
fc = r"E:\gis payannameh\tabusearch2\SUMMARY.gdb\net38_j"
from arcpy import env
fields = ['f1','FIRST_netw']
all_rows = [i[0] for i in arcpy.da.SearchCursor(fc,fields[0])]
min_val = min(all_rows)
print min_val
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 27, 2022 at 10:46

2 Answers 2

3

List all values, use min/max with a lambda function:

import arcpy
fc = r'C:\GIS\ArcMap_default_folder\Default.gdb\my_sample'
fields = ['OID@', 'Shape_Area']
#List all rows
allvalues = [row for row in arcpy.da.SearchCursor(fc, fields)] #[(1, 66901.86709246939), (2, 16444.625346498582), (3, 161485.720859425), ...]
#min(allvalues, key=lambda x: x[1]) #x[1] is to search by second value, in my case 'Shape_Area'
#(105, 1311.0614255410774)
#max(allvalues, key=lambda x: x[1])
#(43, 35160520.119611494)

Or you can sort the list and fetch first and last value using indexes:

allvalues.sort(key=lambda x: x[1])
allvalues[0] #min
allvalues[-1] #max
answered Jan 27, 2022 at 10:59
1

An alternative approach to retrieving all records and using Python to determine the minimum and maximum values and corresponding records is to use an SQL WHERE clause and have the database engine do the work on the back-end. Even for a primitive database engine like with file geodatabases, retrieving a maximum or minimum is supported. (See SQL reference for query expressions used in ArcGIS -- ArcGIS Pro | Documentation)

Python 3.7.11 [MSC v.1927 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import arcpy
>>> import os
>>>
>>> # definte table and records
>>> sgdb = arcpy.env.scratchGDB
>>> ws, tbl_name = os.path.split(arcpy.CreateScratchName(workspace=sgdb))
>>> recs = [
... ["net72.shp", 0.459172],
... ["net48.shp", 0.726129],
... ["net115.shp", 0.729337],
... ["net147.shp", 0.756185],
... ["net88.shp", 0.764957],
... ["net87.shp", 0.768217]
... ]
>>>
>>> # create table
>>> tbl = arcpy.CreateTable_management(ws, tbl_name)
>>> res = arcpy.AddField_management(tbl, "TXT_FLD", "TEXT")
>>> res = arcpy.AddField_management(tbl, "FLT_FLD", "FLOAT")
>>>
>>> # populate table with records
>>> with arcpy.da.InsertCursor(tbl, ["TXT_FLD", "FLT_FLD"]) as cur:
... for rec in recs:
... cur.insertRow(rec)
...
>>> # define SQL WHERE clause to only retrieve maximum and minimum records
>>> sql = (
... "FLT_FLD = (SELECT MAX(FLT_FLD) FROM {0}) OR "
... "FLT_FLD = (SELECT MIN(FLT_FLD) FROM {0})"
... ).format(tbl_name)
>>>
>>> # print maximum and minimum float values along with corresponding file name
>>> with arcpy.da.SearchCursor(tbl, ["TXT_FLD", "FLT_FLD"], sql) as cur:
... for row in cur:
... print(row)
...
('net72.shp', 0.45917201042175293)
('net87.shp', 0.7682170271873474)
>>>
answered Mar 12, 2022 at 23:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.