Pandas:将dbf表转换为数据帧

FaC*_*fee 16 python dbf arcgis dataframe pandas

我想读取dbfArcGIS shapefile的文件并将其转储到pandas数据框中.我目前正在使用dbf包.

我显然能够将dbf文件作为表加载,但是无法弄清楚如何解析它并将其转换为pandas数据帧.这样做的方法是什么?

这是我被困在的地方:

import dbf
thisTable = dbf.Table('C:\\Users\\myfolder\\project\\myfile.dbf')
thisTable.open(mode='read-only')
Run Code Online (Sandbox Code Playgroud)

Python将此语句作为输出返回,我坦率地不知道该怎么做:

dbf.ver_2.Table('C:\\Users\\myfolder\\project\\myfile.dbf', status='read-only')


编辑

我原来的样本dbf:

FID   Shape    E              N
0     Point    90089.518711   -201738.245555
1     Point    93961.324059   -200676.766517
2     Point    97836.321204   -199614.270439
...   ...      ...            ...
Run Code Online (Sandbox Code Playgroud)

Fab*_*nna 25

你应该看一下simpledbf:

In [2]: import pandas as pd

In [3]: from simpledbf import Dbf5

In [4]: dbf = Dbf5('test.dbf')

In [5]: df = dbf.to_dataframe()
Run Code Online (Sandbox Code Playgroud)

这适用于我的一些示例.dbf文件.希望有所帮助.


Phi*_*eal 6

如mmann1123所述,您可以使用geopandas来读取dbf文件。地理熊猫会读取它,即使它可能具有或不具有地理空间数据。

假设您的数据只是表格数据(没有地理坐标),并且您希望读取它并转换为pandas库可以读取的格式,我建议您使用geopandas。

这是一个例子:

import geopandas as gpd

My_file_path_name = r'C:\Users\...file_dbf.dbf'

Table = gpd.read_file(Filename)

import pandas as pd
Pandas_Table = pd.DataFrame(Table)

Keys = list(Table.keys())
Keys.remove('ID_1','ID_2') # removing ID attributes from the Table keys list
Keys.remove('Date') # eventually you have date attribute which you wanna preserve.

DS = pd.melt(Pandas_Table, 
             id_vars =['ID_1','ID_2'], # accepts multiple filter/ID values 
             var_name='class_fito', # Name of the variable which will aggregate all columns from the Table into the Dataframe
             value_name ='biomass (mg.L-1)' , # name of the variable in Dataframe
             value_vars= Keys # parameter that defines which attributes from the Table are a summary of the DataFrame)

# checking your DataFrame:

type(DS)   # should appear something like: pandas.core.frame.DataFrame
Run Code Online (Sandbox Code Playgroud)