Python Geopandas – Get Field Names in Attribute Table

geopandaspythonvector-layer

I have a geopackage file, with a layer called "MyLayer". Within this I have an attribute table with approx 20 attributes.

I can read in each attribute individually like:

import geopandas as gpd

geopkg = gpd.read_file('MyGeoPkg.gpkg', layer='MyLayer')
A = geopkg['AttributeA']

However, I have multiple GPKG files and they may have a different number of attributes, that may have different names.

I'd like to automatically get a list like:

listOfAttributes = ['AttributeA', 'AttributeB', 'AttributeC']

so I could then do:

for l in listOfAttributes:
    data = geopkg[l]

Is there a way of getting a list of the field names from the layer so I can iterate over them?

Best Answer

You effectively want to iterate over columns of GeoDataFrame, which you can get using geopkg.columns.

So assuming you don't want to iterate over geometry field:

for col in geopkg.columns.drop('geometry'):
    data = geopkg[col]
Related Question