Select to view content in your preferred language

Iterating through 7740 fileds to select and delete the fields less than certain values (rainfall data)

1100
4
Jump to solution
06-15-2022 02:03 PM
HadiK
by
Emerging Contributor

I have a point data which has 7740 fields precipitation value in each column for 60 million pixels.

I need to get rid of the columns (Daily Data) which is the date of my attribute table those are has values less than 100 mm of precipitation, find it and delete those columns (7740) . I want to do this to lighten my data for web service.

I want to do it in arcpy and I'm somehow confused how to do it.

Could you please help me with this.

,,,

import os
import arcpy
from arcpy import env

table = "Test_area_cliped"
fields = [f.name for f in arcpy.ListFields(table)]

,,,

I got all the fields now in my code but I do not know how to done it.

 

 

Tags (1)
0 Kudos
2 Solutions

Accepted Solutions
MarkBryant
Regular Contributor

Build a list of the fields where the precipitation is less than 100. There a number of tools that could do this including "Summary Statistics", but I have include a way to do this with numpy. Once you have the list of fields, pass it to the delete fields tool.

import numpy
import arcpy

def get_maximum_field_value(in_table, field_name):
    """Return the maximum value of a field in a table. Using Numpy."""
    data = arcpy.da.TableToNumPyArray(in_table, [field_name])
    return numpy.max(data[field_name])

table = "Test_area_cliped"
fields = [f.name for f in arcpy.ListFields(table)]
delete_fields=[]
for field in fields:
    precipitation = get_maximum_field_value(table, field)
    if precipitation < 100:
        delete_fields.append(field)

arcpy.DeleteField_management(table, delete_fields)

 

Mark.

View solution in original post

MarkBryant
Regular Contributor

The ValueError  could be raised if you have nothing but null values in a column.
You could try changing the function checking for the maximum value to change null values to zeros.

def get_maximum_field_value(in_table, field_name):
    """Return the maximum value of a field in a table. Using Numpy."""
    data = arcpy.da.TableToNumPyArray(in_table, [field_name], null_value=0)
    return numpy.max(data[field_name])

 

Mark.

View solution in original post

4 Replies
MarkBryant
Regular Contributor

Build a list of the fields where the precipitation is less than 100. There a number of tools that could do this including "Summary Statistics", but I have include a way to do this with numpy. Once you have the list of fields, pass it to the delete fields tool.

import numpy
import arcpy

def get_maximum_field_value(in_table, field_name):
    """Return the maximum value of a field in a table. Using Numpy."""
    data = arcpy.da.TableToNumPyArray(in_table, [field_name])
    return numpy.max(data[field_name])

table = "Test_area_cliped"
fields = [f.name for f in arcpy.ListFields(table)]
delete_fields=[]
for field in fields:
    precipitation = get_maximum_field_value(table, field)
    if precipitation < 100:
        delete_fields.append(field)

arcpy.DeleteField_management(table, delete_fields)

 

Mark.
HadiK
by
Emerging Contributor

Thank you for your response and help! 

After input this lines of code I got the following error:

delete_fields=[]
for field in fields:
    precipitation = get_maximum_field_value(table, field)
    if precipitation < 100:
        delete_fields.append(field)

 

ValueError: zero-size array to reduction operation maximum which has no identity

Did I do something in wrong way?

Attached screenshot of my attribute table, it's like this for 7459 days. Of course most of the data have zero values. (Precipitation data)


 

0 Kudos
MarkBryant
Regular Contributor

The ValueError  could be raised if you have nothing but null values in a column.
You could try changing the function checking for the maximum value to change null values to zeros.

def get_maximum_field_value(in_table, field_name):
    """Return the maximum value of a field in a table. Using Numpy."""
    data = arcpy.da.TableToNumPyArray(in_table, [field_name], null_value=0)
    return numpy.max(data[field_name])

 

Mark.
HadiK
by
Emerging Contributor

Thank you so much for your response,

Seems it worked. Good. But I'm not able to apply it or display the new data?1
When I print field it shows me 'lon'

When I print fields it shows me the whole fields name only.

Screenshot_2.jpg

Am I doing something wrong here?! Cause I did exactly as you said 

0 Kudos