4

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?

Steve Maughan
  • 1,174
  • 3
  • 19
  • 30
  • 1
    use this: http://dbfpy.sourceforge.net/ or convert data frame to csv, then csv to dbf . – Iman Mirzadeh Jul 03 '17 at 20:47
  • You could also use [this](https://github.com/GeoDaSandbox/sandbox/blob/master/pyGDsandbox/dataIO.py) alternative with `df2dbf` – DarkCygnus Jul 03 '17 at 21:13
  • What is a dbf file? Is it like a .nc file that xarray uses? – O.rka Aug 27 '18 at 02:38
  • @O.rka the DBF format is an old database format that was popular in the 80's and 90's. It's also part of the shapefile format for storing geographic data, and this format is still popular today. – Steve Maughan Feb 02 '19 at 14:54

2 Answers2

4

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!

Steve Maughan
  • 1,174
  • 3
  • 19
  • 30
0

you should consider using rpy2 to convert your dataframe to an R object then run something like this:

from rpy2.robjects import pandas2ri
pandas2ri.activate()
from rpy2.robjects.packages import importr
base = importr('base')
base.write.dbf(df)

Should work seamlessly, I hope!

zdeeb
  • 142
  • 9