Select to view content in your preferred language

Iterating throgh feature classes in gdb resulting in tables

565
1
08-01-2023 05:45 AM
ZainAhmad
New Contributor

I have been working on a project which includes the creating tables for each feature class in gdp with some info like zeroes, nullsn unknowns. I got successful in making the tables for one class at a time but the plan is iterate through the feature classes, resulting in tables of every feature class  formed in one go. 

 

if you guys can help me, it would be great help. 

Thanks

My code is as follow:

import arcpy
import datetime
import pandas as pd

# Set the workspace to your geodatabase
geodatabase = r"Z:\Team.gdb"
arcpy.env.workspace = geodatabase

def analyze(geodatabase):
dataset = arcpy.ListFeatureClasses(geodatabase)
result = [
def analyze(FeatureClasses):
# Get a list of all fields in the feature class
fields = arcpy.ListFields(FeatureClasses)
output = [
{"Field": f.name, "Type": f.type, "Nulls": 0, "Zeroes": 0, "Unknowns": 0, "DefaultDate": 0}
for f in fields
]
with arcpy.da.SearchCursor(FeatureClasses, [f.name for f in fields]) as cursor:
for row in cursor:
for i, val in enumerate(row):
if val is None:
result[i]["Nulls"] += 1
elif val == 0:
result[i]["Zeroes"] += 1
elif val == "unknown":
result[i]["Unknowns"] += 1
elif result[i]["Type"] == "Date" and val <= datetime.datetime(1929, 12, 31):
result[i]["DefaultDate"] += 1


]



# Create a pandas DataFrame from the results
df = pd.DataFrame(result)
return df

# Display the dateframe as a table in the notebook
display(analyze(geodatabase)

0 Kudos
1 Reply
AlfredBaldenweck
MVP Regular Contributor

This is untested, but basically you had some weird structure going on. I think some of it was typos (random open bracket where you define result as well as random bracket before creating your dataframe).

Basically, you don't need two functions here, especially with the same name. So I combined them into one which takes the GDB as the input and went from there. 

List Feature Classes, then loop over each feature class in the list and do your thing (again, didn't test any of this, so you'll have to tweak). They currently all will feed into the same dataframe.

 

import arcpy
import datetime
import pandas as pd

# Set the workspace to your geodatabase
geodatabase = r"Z:\Team.gdb"
arcpy.env.workspace = geodatabase

def analyze(geodatabase):
    dataset = arcpy.ListFeatureClasses(geodatabase)
    result = {}
    for fc in dataset:
        # Get a list of all fields in the feature class
        fields = arcpy.ListFields(fc)
        output = [{"Field": f.name, 
                    "Type": f.type, 
                    "Nulls": 0, 
                    "Zeroes": 0, 
                    "Unknowns": 0, 
                    "DefaultDate": 0
                    }
                  ]
        with arcpy.da.SearchCursor(FeatureClasses, [f.name for f in fields]) as cursor:
            for row in cursor:
            for i, val in enumerate(row):
                if val is None:
                result[i]["Nulls"] += 1
                elif val == 0:
                result[i]["Zeroes"] += 1
                elif val == "unknown":
                result[i]["Unknowns"] += 1
                elif result[i]["Type"] == "Date" and val <= datetime.datetime(1929, 12, 31):
                result[i]["DefaultDate"] += 1

    # Create a pandas DataFrame from the results
    df = pd.DataFrame(result)
    return df

# Display the dateframe as a table in the notebook
display(analyze(geodatabase)

 

0 Kudos