# Delete fields from a table based on Mean calculation using ArcPy

I have an attribute table with 100 columns (values are float and not NULL; more than 3000 rows). Using ArcGIS 10.6

• I want to calculate the mean value of every field in a shapefile separately (like summary statistic works);
• and use the resulting value for deleting the field: if the Mean value of the field lower than 0.6- delete the field from the table.

How can I use the Mean values of the fields in a for loop in order to delete the unnecessary fields from a table with python?

• What have you tried? What are your python skills like? This would be s relatively simple coding exercise. Jan 23 at 16:41
• Basic python skill. I work with model builder mainly, but for this I can not find any solution.
– GisT
Jan 23 at 17:24

This would be difficult and I suspect overtly complex if implemented in modelbuilder, a python solution would be much easier to run.

Here is the code that will achieve what you need:

``````import arcpy

table = r"C:\Scratch\fGDB_Scratch.gdb\test"

# Creates an array using all fields in table
array = arcpy.da.TableToNumPyArray(table,"*")

# Create a list of field names
lstFields = list(array.dtype.names)

# Remove OBJECTID from list
# You add other fields to remove here, eg. text fields
lstFields.remove("OBJECTID")

# Compute mean and if it falls below limit add to delete list
print("identifying fields to delete...")
toDelete = list()
for fn in lstFields:
if array[fn].mean() < 3.6:
toDelete.append(fn)

# Drop fields
print("Deleting fields...")
arcpy.DeleteField_management(table,toDelete)

print("Done!")
``````

Once you have modified this code to your case drop this code into the Python console window in ArcMap or run it from an IDE.

In my test scenario I have a table and I'm checking the mean of the fields to see if they are below the value of 3.6, obviously adapt it to your scenario. I would recommend you do this from a backed up version in case your logic was wrong. My test table was this: • Thank you!!! It works fine!
– GisT
Jan 23 at 21:26