-
-
Save mhweber/a2d5863fcc4d52376481 to your computer and use it in GitHub Desktop.
Import DBF file to Pandas data frame in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pysal as ps | |
import pandas as pd | |
''' | |
Arguments | |
--------- | |
dbfile : DBF file - Input to be imported | |
upper : Condition - If true, make column heads upper case | |
''' | |
def dbf2DF(dbfile, upper=True): #Reads in DBF files and returns Pandas DF | |
db = ps.open(dbfile) #Pysal to open DBF | |
d = {col: db.by_col(col) for col in db.header} #Convert dbf to dictionary | |
#pandasDF = pd.DataFrame(db[:]) #Convert to Pandas DF | |
pandasDF = pd.DataFrame(d) #Convert to Pandas DF | |
if upper == True: #Make columns uppercase if wanted | |
pandasDF.columns = map(str.upper, db.header) | |
db.close() | |
return pandasDF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment