6

I have a set of tables that I have exported to a file geodatabase. I would like to read it into python as a pandas dataframe. When I try to read the file directly using pd.read_csv

import pandas as pd

df = pd.read_csv(r'path\to\your\gdb.gdb\file')

I get the error:

IOError: File path\to\your\gdb.gdb\file does not exist

How can I read the table into a pandas dataframe?

3 Answers 3

8

You can use da.SearchCursor:

import arcpy
import pandas as pd
table = r"C:\GIS\ArcMap_default_folder\Default.gdb\grids"
columns = [f.name for f in arcpy.ListFields(table) if f.type!="Geometry"] #List the fields you want to include. I want all columns except the geometry
df = pd.DataFrame(data=arcpy.da.SearchCursor(table, columns), columns=columns)
1

ArcGIS has a function that converts Table to a NumPy Array and FeatureClass To NumPy Array, so you need to convert to a NumPy Array and then convert to a Pandas Dataframe

There's a code example,

import arcpy
import pandas as pd

def arcgis_table_to_df(in_fc, input_fields=None, query=""):
    """Function will convert an arcgis table into a pandas dataframe with an object ID index, and the selected
    input fields using an arcpy.da.SearchCursor.
    :param - in_fc - input feature class or table to convert
    :param - input_fields - fields to input to a da search cursor for retrieval
    :param - query - sql query to grab appropriate values
    :returns - pandas.DataFrame"""
    OIDFieldName = arcpy.Describe(in_fc).OIDFieldName
    if input_fields:
        final_fields = [OIDFieldName] + input_fields
    else:
        final_fields = [field.name for field in arcpy.ListFields(in_fc)]
    data = [row for row in arcpy.da.SearchCursor(in_fc,final_fields,where_clause=query)]
    fc_dataframe = pd.DataFrame(data,columns=final_fields)
    fc_dataframe = fc_dataframe.set_index(OIDFieldName,drop=True)
    return fc_dataframe

def arcgis_table_to_dataframe(in_fc, input_fields, query="", skip_nulls=False, null_values=None):
    """Function will convert an arcgis table into a pandas dataframe with an object ID index, and the selected
    input fields. Uses TableToNumPyArray to get initial data.
    :param - in_fc - input feature class or table to convert
    :param - input_fields - fields to input into a da numpy converter function
    :param - query - sql like query to filter out records returned
    :param - skip_nulls - skip rows with null values
    :param - null_values - values to replace null values with.
    :returns - pandas dataframe"""
    OIDFieldName = arcpy.Describe(in_fc).OIDFieldName
    if input_fields:
        final_fields = [OIDFieldName] + input_fields
    else:
        final_fields = [field.name for field in arcpy.ListFields(in_fc)]
    np_array = arcpy.da.TableToNumPyArray(in_fc, final_fields, query, skip_nulls, null_values)
    object_id_index = np_array[OIDFieldName]
    fc_dataframe = pd.DataFrame(np_array, index=object_id_index, columns=input_fields)
    return fc_dataframe

source

0

I would use the GeoAccessor from arcgis...

import pandas as pd
from arcgis.features import GeoAccessor
df = pd.DataFrame(GeoAccessor.from_table(r'Path to table'))

Assuming you would also like to export the table back to a file geodatabase you can use

import pandas as pd
from arcgis.features import GeoAccessor
df = pd.DataFrame(GeoAccessor.from_table(r'Path to table'))
df.spatial.to_table(r'Path\tablename')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.