Feature Layer to DataFrame

1241
4
Jump to solution
04-29-2019 03:31 PM
JillKelly
New Contributor II

I understand that SpatialDataFrame has been replaced by a "spatially enabled dataframe."  It looks like the former was a function in arcgis.features while the latter goes through pandas.  I cannot get either to work!  On advice from another thread, I rolled back my pandas to 0.23.4, but that has not helped (arcgis api version 1.6).  Using tab-complete, pandas.DataFrame.spatial does not appear to have any methods.  Apparently arcgis.features.GeoAccessor has a .from_layer method that yields a pandas dataframe, but I wasn't able to define a feature layer from a url. 

Old Way (SpatialDataFrame):

New Way ("spatially enabled dataframe" via pandas):

Third way (GeoAccessor):

from arcgis.features import FeatureLayer, GeoAccessor
FL = FeatureLayer('http://[my institution].maps.arcgis.com/home/item.html?id=fbcae48fb3e64ad886ee84ddb225f39a')
df = GeoAccessor.from_layer(FL)

That FL does not have properties and cannot be mapped, so it's no surprise from_layer fails as well.

Any thoughts?

0 Kudos
1 Solution

Accepted Solutions
HåkonDreyer
Esri Contributor

If I haven't misunderstood your problem I guess layer.query().sdf is what you are looking for.

from arcgis import GIS
gis = GIS()
_content = gis.content.search("Ziekenhuisen")
_layer = _content[0].layers[0]
_layer.query().sdf

View solution in original post

4 Replies
HåkonDreyer
Esri Contributor

If I haven't misunderstood your problem I guess layer.query().sdf is what you are looking for.

from arcgis import GIS
gis = GIS()
_content = gis.content.search("Ziekenhuisen")
_layer = _content[0].layers[0]
_layer.query().sdf

View solution in original post

JillKelly
New Contributor II

That *is* what I needed.  Apparently you cannot assign it to a variable, which (mistakenly) led me to believe that approach was out of date.  Really what I needed is:

df = pandas.DataFrame(_layer.query().sdf)

And now I have that -- thanks!

0 Kudos
HåkonDreyer
Esri Contributor

Glad to be of help, and actually it's even easier than that:

codesample

JoshuaBixby
MVP Esteemed Contributor

Please mark the reply as correct to close out the question, thanks.

0 Kudos