简体   繁体   中英

export to csv and read multiIndex dataframe pandas

I need to export to csv and then import again a DataFrame that looks like this:

    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'. The multiIndex looks like this

 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(). 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:

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.

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().

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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