In [259]: store = HDFStore('store.h5')
In [260]: print(store)
<class 'pandas.io.pytables.HDFStore'>
File path: store.h5
Empty
Objects can be written to the file just like adding key-value pairs to a dict:
In [261]: np.random.seed(1234)
In [262]: index = date_range('1/1/2000', periods=8)
In [263]: s = Series(randn(5), index=['a', 'b', 'c', 'd', 'e'])
In [264]: df = DataFrame(randn(8, 3), index=index,
.....: columns=['A', 'B', 'C'])
.....:
In [265]: wp = Panel(randn(2, 5, 4), items=['Item1', 'Item2'],
.....: major_axis=date_range('1/1/2000', periods=5),
.....: minor_axis=['A', 'B', 'C', 'D'])
.....:
# store.put('s', s) is an equivalent method
In [266]: store['s'] = s
In [267]: store['df'] = df
In [268]: store['wp'] = wp
# the type of stored data
In [269]: store.root.wp._v_attrs.pandas_type
Out[269]: 'wide'
In [270]: store
Out[270]:
<class 'pandas.io.pytables.HDFStore'>
File path: store.h5
/df frame (shape->[8,3])
/s series (shape->[5])
/wp wide (shape->[2,5,4])
这一切都与用例有关。
我的经验法则是:如果两个数据之间存在依赖或关系,则使用SQLite;如果只是平面数据文件,则使用CSV(或其他“平面”格式)。最简单的可行解决方案通常也是最可靠的解决方案。
(注意:确保CSV格式正确。没有人喜欢在糟糕的CSV实现中进行操作。)
XML被设计为主流和相对“轻量级”。JSON是另一个流行的选择,但更适合对象建模而不是数据存储。
如果您需要关系查询功能,则MySQL是一个好的选择。