I try to code spatial join tool and delete some fields in the result. to delete the fields i list the fields of source layer and the result layer then use the list of differences in the two list and usedelete field management tool.it's working but not good solution.If i can set the Spatial Join field mapping to remain the source layer fields and only the "Distance_To_Next" field of the join layer, i get rid of some codes and iterates but don't know how to set the field mapping in the code.
Scripting spatial join in ArcPy to set field attribute in field mapping object? is the only result that i could find in the stack but not my answer.
import arcpy arcpy.SpatialJoin_analysis("Point1", "point2","JoinResult","JOIN_ONE_TO_ONE","#","#","INTERSECT") listfields1 = [fraft.name for fraft in arcpy.ListFields("Point1")] listfields2 = [fraftv.name for fraftv in arcpy.ListFields("JoinResult")] diff_fields = [i for i in listfields2 if not i in listfields1] for field in diff_fields: if field != "Distance_To_Next": arcpy.DeleteField_management("JoinResult",field)