简体   繁体   English

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

[英]export to csv and read multiIndex dataframe pandas

I need to export to csv and then import again a DataFrame that looks like this: 我需要导出到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

where the row index is represented by the timestamp and the first 39 columns are subcolumns of 'price' while the remaining ones are on the same level of 'price'. 其中行索引由时间戳表示,前39列是“价格”子列,其余的列在“价格”的同一级别上。 The multiIndex looks like this 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]])

I have no idea on how to preserve this structure easily while exporting with df.to_csv() and importing with df.read_csv(). 我不知道如何在使用df.to_csv()导出和使用df.read_csv()导出时轻松地保留此结构。 All my attempts have been a mess so far. 到目前为止,我所有的尝试都一团糟。

EDIT: if I simply use as suggested pd.to_csv("/", index=True) and then I read it back with read_csv("/"), I get: 编辑:如果我只是按建议使用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

where the second layer of the header became the first row of the dataFrame. 标头的第二层变为dataFrame的第一行。

EDIT2: Nevermind, I've just discovered hdf5 and apparently, contrary to csv, it preserves the structure even with multiIndex without additional work, so I will use pd.to_hdf(). EDIT2:没关系,我刚刚发现了hdf5,显然,与csv相反,即使使用multiIndex,它也可以保留结构,而无需进行其他工作,因此我将使用pd.to_hdf()。

我认为如果您使用-pd.to_csv(“ /”,index = True)它将其与索引一起保存,然后像往常一样读回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM