Write Pandas Dataframe to DBF File?

Viewed 4741

I'm working with shapefiles, which have the attribute data stored in a DBF file. I need to manipulate the attributes using Pandas, and write the new DBF data back to disk.

Unfortunately Pandas dataframes don't have a df.to_dbf method (whereas R does). I've hunted around but don't seem to see any obvious ways to accomplish this task.

How do I save a pandas dataframe as a DBF file?

2 Answers

By far the easiest way to load, manipulate and save the dbf file associated with a shapefile is to use the GeoPandas library. This can load the dbf as a dataframe:

gf = gpd.read_file('some_shapefile.shp')

You can then manipulate all of the data using standard Pandas commands and export the data back out as a Shapefile (or other geographic file) using the following command:

gf.to_file('export.shp')

Awesome!

Related