[GIS] Selecting certain fields from feature class to create new feature class in ArcPy

arcgis-10.1arcpyfeaturesfields-attributes

I am stuck on how to create multiple features classes from one feature class using a select few fields.
I have the following attribute table for a shapefile.
enter image description here

I would like to select the 'USAF', 'COUNT', 'ADMIN_1' fields and append one year field from 2000, 2001, 2002…2014), such that the attribute fields of the new feature classes will be:

  1. 'USAF', 'COUNT', 'ADMIN_1', '2000'

  2. 'USAF', 'COUNT', 'ADMIN_1', '2001'

  3. 'USAF', 'COUNT', 'ADMIN_1', '2002'

    . . .

Till 2014.

I have been trying to work it out using modelbuilder and a python script (separately) and haven't been close to successful. Here is a script I wrote to test on the first year, but the resultant shapefile is identical to the input

 #import the arcpy site-package
 import arcpy
 from arcpy import env
 env.workspace = "Z:\\GEOG517\\siu850437305\\Project\\Results" #set current workspace
 env.overwriteOutput = True #allow outputs to be overwritten

 #List variables
 in_file = "Z:\\GEOG517\\siu850437305\\Project\\shapefiles\\sahel_ppt.shp"

 arcpy.MakeFeatureLayer_management(in_file, "stationslyr.shp")# Make a layer   from the feature class
 stations = "stationslyr"

 #Create field list iterate through using for loop
 fields = arcpy.ListFields(stations)
 for field in fields:
     for field.name == '2000':
         if field.name == "2000":
             arcpy.SelectLayerByAttribute_management(stations, "new_selection" )
             arcpy.CopyFeatures_management(stations, "yr2000")# Write the selected features to a new featureclass
             print ("Created yr2000")
             break

Then tried to append the years on using:

field_names = []
fields = arcpy.ListFields(stations)
for field in fields:
     field_names.append(field.name)

but that failed too!
I am not even sure there is a tool to do this. I am new to python scritping for geoprocessing tasks so I am not looking for a really complex method just a simple and straightforward way to do this.

Best Answer

I found the answer here, it requires field mapping:
copy feature class and retain selected fields with arcpy
The documentation pages (for 10.1) are here:
field mappings, which are a collection of...
field map objects

There seem to be some typo issues in that first link's answer (and I didn't find it sufficiently explained), so I got it to work this way:

# feature class I want to copy from
existingFC = r'T:\GIS\Config\DatabaseConnections\GISP ReadOnly on 58u.sde\AGL.GasFacility\AGL.GasMain'
# list of fields I want to keep (capitalization counts!)
myfields = ['DATECREATED', 'DATEMODIFIED', 'SUBTYPECD', 'MEASUREDLENGTH', 'TRANSMISSION', 'NOP', 'FACILITYID', 'DATEPOSTED']
# create an empty field mapping object
mapS = arcpy.FieldMappings()
# for each field, create an individual field map, and add it to the field mapping object
for field in myfields :
    map = arcpy.FieldMap()
    map.addInputField(gasMain, field)
    mapS.addFieldMap(map)
# copy the feature class using the fields you want
newCopy = arcpy.FeatureClassToFeatureClass_conversion(existingFC, myDataBase, "new feature class name", "#", mapS)

The alternative answer I've found (which doesn't satisfy my needs because the process takes to long for the datasets I'm working with) is this:
drop fields after copying

Related Question