Search code examples
pythonpandasdbf

How to read dbf file in python and convert it to dataframe


I am trying to read a dbf file using simpledbf library and convert to to dataframe for further processing.

from simpledbf import Dbf5
dbf = Dbf5(r"C:\Users\Prashant.kumar\Downloads\dbf\F1_1.DBF")
df1 = dbf.to_dataframe()

Unfortunately, I am getting the following error. enter image description here

I tried to find a solution but couldn't find a resolution, nor I can find an alternative way to convert the dbf file to a dataframe for post processing.

Here is the file https://mega.nz/folder/gKIBUKIa#rE7TmE5FToLzCblMhLLFbw

Is there a way to read this dbf to python as a dataframe?


Solution

  • Use dbfread instead of simpledbf:

    # pip install dbfread
    from dbfread import DBF
    from pandas import DataFrame
    
    dbf = DBF('F1_1.DBF')
    df = DataFrame(iter(dbf))
    

    Output:

    >>> df
         RESPONDENT                                         RESPONDEN2 RESPONDEN3 STATUS  FORM_TYPE  STATUS_DAT SORT_NAME PSWD_GEN _NullFlags
    0             1                             AEP Generating Company                 A          0  1990-01-01                       b'\x00'
    1             2                              ALABAMA POWER COMPANY                 A          0  2000-05-03                       b'\x00'
    2             3            Alaska Electric Light and Power Company                 A          0  1990-01-01                       b'\x00'
    3             4                        Alcoa Power Generating Inc.                 A          0  1990-01-01                       b'\x00'
    4             5                   THE ALLEGHENY GENERATING COMPANY                 A          0  1990-01-01                       b'\x00'
    ..          ...                                                ...        ...    ...        ...         ...       ...      ...        ...
    389         538                                    DesertLink, LLC                 A         -1  2020-11-17                       b'\x00'
    390         539  NextEra Energy Transmission MidAtlantic Indian...                 A         -1  2020-12-03                       b'\x00'
    391         540                      Wilderness Line Holdings, LLC                 A         -1  2020-12-15                       b'\x00'
    392         541                McKenzie Electric Cooperative, Inc.                 A         -1  2021-04-19                       b'\x00'
    393         542               LS Power Grid New York Corporation I                 A          0  2021-08-27                       b'\x00'
    
    [394 rows x 9 columns]