导出到csv并读取multiIndex数据框熊猫

时间:2019-01-21 15:29:39

标签: python pandas export-to-csv

我需要导出到csv,然后再次导入一个看起来像这样的DataFrame:

    price   ................................................................................................................... hold buy    balance long_size   short_size  minute  hour    day week    month
                       close    high    low open    CCI12   ROC12   CCI15   ROC15   CCI21   ROC21   ...                                     
Time                                                                                    
2015-01-02 14:20:00 97.8515 97.8595 97.8205 97.8345 91.168620   0.000557    95.323467   0.000394    68.073065   0.000348    ... 0.0 0.0 0.0 0.0 0.0 8.660254e-01    -0.500000   0.974928    1.205367e-01    5.000000e-01

其中,行索引由时间戳表示,前39列是“价格”子列,其余的列在“价格”的同一级别上。 multiIndex看起来像这样

 MultiIndex(levels=[['price', 'tick_counts', 'sell', 'hold', 'buy', 'balance', 'long_size', 'short_size', 'minute', 'hour', 'day', 'week', 'month'], [0, 'close', 'high', 'low', 'open', 'CCI12', 'ROC12', 'CCI15', 'ROC15', 'CCI21', 'ROC21', 'CCI30', 'ROC30', 'CCI40', 'ROC40', 'CCI100', 'ROC100', 'SMA12', 'EWMA12', 'SMA21', 'EWMA21', 'SMA26', 'EWMA26', 'SMA50', 'EWMA50', 'SMA100', 'EWMA100', 'SMA200', 'EWMA200', 'MACD', 'UpperBB10', 'LowerBB10', 'UpperBB20', 'LowerBB20', 'UpperBB30', 'LowerBB30', 'UpperBB40', 'LowerBB40', 'UpperBB50', 'LowerBB50', '']],
           labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 0, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40]])

我不知道如何在使用df.to_csv()导出并使用df.read_csv()导入时轻松保留此结构。到目前为止,我所有的尝试都一团糟。

编辑:如果我只是按照建议使用pd.to_csv(“ /”,index = True),然后用read_csv(“ /”)读回,我得到:

Unnamed: 0  price   price.1 price.2 price.3 price.4 price.5 price.6 price.7 price.8 ... hold    buy balance long_size   short_size  minute  hour    day week    month
0   NaN close   high    low open    CCI12   ROC12   CCI15   ROC15   CCI21   ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1   Time    NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2   2015-01-02 14:20:00 97.85149999999999   97.8595 97.82050000000001   97.83449999999999   91.16862020296143   0.0005572768080819476   95.32346677471595   0.0003936082115872622   68.07306512447788   ... 0.0 0.0 0.0 0.0 0.0 8.660254e-01    -0.500000   0.974928    1.205367e-01    5.000000e-01

标头的第二层成为dataFrame的第一行。

EDIT2:没关系,我刚刚发现了hdf5,显然,与csv相反,即使使用multiIndex,它也可以保留结构,而无需进行其他工作,因此我将使用pd.to_hdf()。

1 个答案:

答案 0 :(得分:0)

我认为如果您使用-pd.to_csv(“ /”,index = True) 它用索引保存它,然后照常读取。