How do I convert a .dbf file into a Pandas DataFrame?

喜夏-厌秋 提交于 2021-01-05 08:58:34

问题


I have a .dbf file that I would like to convert into a pandas DataFrame, but DataFrames aren't able to directly convert the data.


回答1:


Using my dbf library, the following function will do the job:

def dbf_to_dataframe(filename):
    """
    converts the dbf table at filename into a Panda's DataFrame
    data types and field names are preserved
    """
    import dbf
    import numpy as np
    import pandas as pd
    from datetime import date, datetime, time
    names = []
    types = []
    with dbf.Table(filename):
        for name in table.field_names:
            ftype, size, decimals, _ = table.field_info(name)
            ftype = chr(ftype)
            if ftype in 'GP':
                continue
            if ftype == 'N' and decimals:
                ftype = 'F'
            dtype = {
                    'B': 'float64',
                    'C': 'string',
                    'D': 'datetime64[ns]',
                    'F': 'float64',
                    'I': 'int64',
                    'L': 'boolean',
                    'M': 'string',
                    'N': 'int64',
                    'T': 'datetime64[ns]',
                    'Y': 'float64',
                    }[ftype]
            names.append(name)
            types.append(dtype)
        series = [[] for _ in names]
        for rec in table:
            for i, value in enumerate(rec):
                if isinstance(value, date):
                    value = datetime.combine(value, time())
                elif value is None:
                    value = np.nan
                series[i].append(value)
        data_recs = dict(
                (n, pd.Series(s, dtype=t))
                for n, s, t in zip(names, series, types)
                )
        return pd.DataFrame(data_recs)


来源:https://stackoverflow.com/questions/65485987/how-do-i-convert-a-dbf-file-into-a-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!