我认为,如果您所有的 DF 都具有相同的形状,那么将您的数据存储为 pandas.Panel 而不是 DF 列表会更自然 - 这就是 pandas_datareader 的工作方式
import io
import pandas as pd
df1 = pd.read_csv(io.StringIO("""
Stock,Year,Profit,CountPercent
AAPL,2012,1,38.77
AAPL,2013,1,33.33
"""
))
df2 = pd.read_csv(io.StringIO("""
Stock,Year,Profit,CountPercent
GOOG,2012,1,43.47
GOOG,2013,1,32.35
"""
))
df3 = pd.read_csv(io.StringIO("""
Stock,Year,Profit,CountPercent
ABC,2012,1,40.0
ABC,2013,1,32.35
"""
))
store = pd.HDFStore('c:/temp/stocks.h5')
# i had to drop `Stock` column and make it Panel-Axis, because of ERROR:
# TypeError: Cannot serialize the column [%s] because its data contents are [mixed-integer] object dtype
# when saving Panel to HDFStore ...
p = pd.Panel({df.iat[0, 0]:df.drop('Stock', 1) for df in [df1,df2,df3]})
store = pd.HDFStore('c:/temp/stocks.h5')
store.append('stocks', p, data_columns=True, mode='w')
store.close()
# read panel from HDFStore
store = pd.HDFStore('c:/temp/stocks.h5')
p = store.select('stocks')
商店:
In [18]: store
Out[18]:
<class 'pandas.io.pytables.HDFStore'>
File path: c:/temp/stocks.h5
/stocks wide_table (typ->appendable,nrows->6,ncols->3,indexers->[major_axis,minor_axis],dc->[AAPL,ABC,GOOG])
面板尺寸:
In [19]: p['AAPL']
Out[19]:
Year Profit CountPercent
0 2012.0 1.0 38.77
1 2013.0 1.0 33.33
In [20]: p[:, :, 'Profit']
Out[20]:
AAPL ABC GOOG
0 1.0 1.0 1.0
1 1.0 1.0 1.0
In [21]: p[:, 0]
Out[21]:
AAPL ABC GOOG
Year 2012.00 2012.0 2012.00
Profit 1.00 1.0 1.00
CountPercent 38.77 40.0 43.47