I have a point shapefile that I ran the ArcGIS geoprocessing tool Near on resulting in a table of which points are near one another. The two columns that I am interested in are pointid and NEAR_FID. I want to compare these two columns and keep only the records where the pointid is noted first. For example:

given the following table

pointid NEAR_ID
1       2
2       1

I would only expect the feature corresponding to pointid 1 to be returned. How would I do this?

  • 2
    Do you mean where pointid has the lesser of the two values?
    – Aaron
    Commented Aug 19, 2016 at 18:54
  • @Aaron oh gosh. I've been at this for too long today. You're right. Please place that down as an answer so I can give you karma or whatever it's called on here...
    – jwx
    Commented Aug 19, 2016 at 19:04
  • 1
    I think it is about finding twins gis.stackexchange.com/questions/202766/…
    – FelixIP
    Commented Aug 19, 2016 at 20:51

1 Answer 1


You can accomplish this with Select (Analysis) with the following SQL query " pointid < NEAR_ID ".

import arcpy

in_shp = r'C:\path\to\inshp.shp'
out_shp = r'C:\path\to\outshp.shp'

arcpy.Select_analysis(in_shp, out_shp, where_clause = " pointid  < NEAR_ID ")

Alternatively, if you need to run a selection on a feature layer for further analysis (i.e. without creating an output featureclass) use Select Layer By Attribute (Data Management). For example:

import arcpy

in_shp = r'C:\path\to\inshp.shp'

arcpy.MakeFeatureLayer_management(in_shp, "shp_lyr")
arcpy.SelectLayerByAttribute_management("shp_lyr", "ADD_TO_SELECTION", where_clause = " pointid  < NEAR_ID ")

# Run some analysis...
  • I believe there is a flaw in this approach that could have a drastic impact on the results. The Near tool is giving the closest point for each point in the dataset. In some cases, these happen to result in pairs - but they won't always do so and @jwx should be aware of this and how Near is working. Add PointID 7 Near_FID 2 to the list. It will fail this selection criteria and be eliminated, even if there are no closer points to 7. Unless 7 is closest to another, all record of it will be lost. And if it did stay that record would falsely indicate 7's closest match. See Felix's comment.
    – Chris W
    Commented Feb 8, 2017 at 20:26

Your Answer

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

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