I found this example and made it even simpler. FC equals your feature class, fieldList equals what fields you want to keep. My code is below.
#Define function to take an input layer and output a layer with only a defined field listdef filter_fields(FC, fieldList):# List input fields fields= arcpy.ListFields(FC)# Create a fieldinfo objects fieldinfo = arcpy.FieldInfo()# Iterate over input fields, add them to the FieldInfo and hide them if # they aren't in the list of fields to be kept for field in fields:if not field.name in fieldList:fieldinfo.addField(field.name, field.name, "HIDDEN", "")# Copy features to a layer using the FieldInfo temp = "temp" arcpy.MakeFeatureLayer_management(FC, temp, "", "", fieldinfo)#Call the functionoutFeature= "admin3_AllJoined"filter_fields(outFeature,["Admin3_admin1Name","Admin3_admin3Name","Education_Table_Education_Percent","Education_Table_Education","Food_Table_Food_Percent","Food_Table_Food_Fraction","Healthcare_Table_Healthcare_Percent","Healthcare_Table_Healthcare_Fraction","Hygiene_Table_Hygiene_Percent","Hygiene_Table_Hygiene_Fraction","NFI_Table_NFI_Percent","NFI_Table_NFI_Fraction","Nutrition_Table_Nutrition_Percent","Nutrition_Table_Nutrition_Fraction","Protection_Table_Protection_Percent","Protection_Table_Protection_Fraction","Shelter_Table_Shelter_Percent","Shelter_Table_Shelter_Fraction","Water_Table_count","Water_Table_Water_Percent","Water_Table_Water_Fraction"])#arcpy.CopyFeatures_management(layerName,outFeature)arcpy.CopyFeatures_management("temp",outFeature2)
A subset of fields can be made unavailable in the new layer by using the Field Info control's visible property. The third column in the control provides a dropdown option to specify whether a field will be visible or hidden in the new layer. The default is TRUE. Selecting FALSE will hide that field. You cannot use the hidden fields in a workflow if the newly created layer is input to a subsequent process or tool. If the output is saved to disk, only the fields listed as visible will appear in the new data.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.