确定 pandas HDF文件中DataFrame的格式 [英] Determine format of a DataFrame in pandas HDF file
本文介绍了确定 pandas HDF文件中DataFrame的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个HDF文件'file.h5',保存到其中的pandas DataFrame(或系列)的键名是'df'.如何确定"df"以哪种格式(即固定"或表格")保存到文件中?
There is an HDF file 'file.h5' and the key name of a pandas DataFrame (or a Series) saved into it is 'df'. How can one determine in what format (i.e. ‘fixed’ or ‘table’) was 'df' saved into the file?
谢谢您的帮助!
推荐答案
有点晚了,但也许有人会觉得有用.
A bit late but maybe someone else may find it helpful.
您可以解析 HDFStore.info()
. table
格式的对象的类型为appendable
:
You can parse the output of HDFStore.info()
. Objects in table
format have the type appendable
:
>>> print(h5_table.info())
<class 'pandas.io.pytables.HDFStore'>
File path: /tmp/df_table.h5
/df frame_table (typ->appendable,nrows->2,ncols->2,indexers->[index],dc->[])
>>> print(h5_fixed.info())
<class 'pandas.io.pytables.HDFStore'>
File path: /tmp/df_fixed.h5
/df frame (shape->[2,2])
这是一个最小的示例(即对丢失的文件或密钥没有错误处理)示例:
This is a minimal (i.e. without error handling for missing file or key) example:
def get_hd5_format(path, key):
with pd.HDFStore(path) as store:
info = store.info()
return 'table' if 'typ->appendable' in next(k for k in info.splitlines()[2:] if k.startswith('/'+key)).split()[2] else 'fixed'
示例用法:
>>> get_hd5_format('/tmp/df_table.h5', 'df')
'table'
>>> get_hd5_format('/tmp/df_fixed.h5', 'df')
'fixed'
这篇关于确定 pandas HDF文件中DataFrame的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文