首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pytables检查列是否存在

Pytables检查列是否存在
EN

Stack Overflow用户
提问于 2016-07-12 06:41:06
回答 2查看 580关注 0票数 2

是否可以使用Pytable(或Pandas)来检测hdf文件的表是否包含特定的列?要加载hdf文件,我使用:

代码语言:javascript
复制
from pandas.io.pytables import HDFStore
# this doesn't read the full file which is good
hdf_store = HDFStore('data.h5', mode='r')
# returns a "Group" object, not sure if this could be used...
hdf_store.get_node('tablename')

我也可以直接使用Pytable而不是Pandas。目的不是加载hdf文件的所有数据,因为这些文件可能很大,我只想确定某个列是否存在。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-12 08:07:08

我可能已经找到了一个解决方案,但我不确定(1)它为什么工作,(2)这是否是一个健壮的解决方案。

代码语言:javascript
复制
import tables
h5 = tables.openFile('data.h5', mode='r')
df_node = h5.root.__getattr__('tablename')
# Not sure why `axis0` contains the column data, but it seems consistent
# with the tested h5 files.
columns = df_node.axis0[:]

columns包含一个具有所有列名的numpy数组。

票数 2
EN

Stack Overflow用户

发布于 2017-08-13 14:32:05

对于Pandas0.20.3和PyTables 3.3.0 (使用Pandas创建的HDF文件),接受的解决方案对我不起作用。然而,这样做是可行的:

代码语言:javascript
复制
pd.HDFStore('data.hd5', mode='r').get_node('/path/to/pandas/df').table.colnames
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38321684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档