Select to view content in your preferred language

How to count records in hosted feature layer with Python

428
2
12-11-2023 06:20 PM
JoseSanchez
Regular Contributor

Hello,

Is there a way to count records from a hosted feature class in AGOL using Python?

Thanks

 

0 Kudos
2 Replies
GeeteshSingh07
Occasional Contributor II

@JoseSanchez, might be late response, but I hope, this answers your question:

from arcgis.gis import GIS
gis = GIS("https://www.arcgis.com", "username", "password")

# Access your feature layer
item = gis.content.get("<ITEM ID>")
layer = item.layers[0]  
result = layer.query(return_count_only=True)

# Print the count of features
print("Number of features:", result)

 

GeeteshSingh07_0-1709277506470.png

 

0 Kudos
JoseSanchez
Regular Contributor

Thank you @GeeteshSingh07 

 

This is  how I did  it:

# Get FeatureLayerCollection
myCollection= signInObject.GIS.content.get('#######')

# Obtain feature layer from FeatureLayerCollection
featureLayer = myCollection.layers[0]
# Query a Feature Layer to get a Feature Set
feature_set = featureLayer.query(where="1=1")
# Assign a variable to the list of features in the Feature Set
feature_list = feature_set.features
# Count records
print(len(feature_list))

 

Thank you