Python 保存和加载多索引数据框并保留柱结构

Python 保存和加载多索引数据框并保留柱结构,python,pandas,csv,multi-index,Python,Pandas,Csv,Multi Index,我无法找到正确保存和检索多索引数据帧的方法,从而保留多索引列结构。对于可再现的示例: 您没有提供可用的样本数据,但我相当确定您需要做的只是将头=[0,1]和索引col=0作为读取csv的参数传递给您没有提供可用的样本数据,但我相当确定您需要做的就是传递头=[0,1],和index\u col=0作为read\u csv的参数 在read\u csv中使用header和index\u col参数将获得所需的内容 toy_data.to_csv('sample.csv') pd.read_csv(

我无法找到正确保存和检索多索引数据帧的方法,从而保留多索引列结构。对于可再现的示例:


您没有提供可用的样本数据,但我相当确定您需要做的只是将
头=[0,1]
索引col=0
作为
读取csv

的参数传递给
您没有提供可用的样本数据,但我相当确定您需要做的就是传递
头=[0,1]
,和
index\u col=0
作为
read\u csv
的参数 在
read\u csv
中使用
header
index\u col
参数将获得所需的内容

toy_data.to_csv('sample.csv')
pd.read_csv('sample.csv', header=[0, 1], index_col=[0])

Company       GOOG          FB     
Indicators  Shares  ROE Shares  ROE
Quarter_end                        
2018-03-24     NaN  NaN    NaN  NaN
2018-03-25     NaN  NaN    NaN  NaN
2018-03-30     NaN  NaN    NaN  NaN
2018-03-31     1.0  2.0    3.0  4.0
2018-04-01     NaN  NaN    NaN  NaN

read\u hdf
保存到
hdf
可能是更好的选择

toy_data.to_hdf('sample.h5', 'toy_key')
pd.read_hdf('sample.h5', 'toy_key')

Company       GOOG          FB     
Indicators  Shares  ROE Shares  ROE
Quarter_end                        
2018-03-24     NaN  NaN    NaN  NaN
2018-03-25     NaN  NaN    NaN  NaN
2018-03-30     NaN  NaN    NaN  NaN
2018-03-31     1.0  2.0    3.0  4.0
2018-04-01     NaN  NaN    NaN  NaN

安装程序
read\u csv
read\u csv
中使用
header
index\u col
参数将获得所需的内容

toy_data.to_csv('sample.csv')
pd.read_csv('sample.csv', header=[0, 1], index_col=[0])

Company       GOOG          FB     
Indicators  Shares  ROE Shares  ROE
Quarter_end                        
2018-03-24     NaN  NaN    NaN  NaN
2018-03-25     NaN  NaN    NaN  NaN
2018-03-30     NaN  NaN    NaN  NaN
2018-03-31     1.0  2.0    3.0  4.0
2018-04-01     NaN  NaN    NaN  NaN

read\u hdf
保存到
hdf
可能是更好的选择

toy_data.to_hdf('sample.h5', 'toy_key')
pd.read_hdf('sample.h5', 'toy_key')

Company       GOOG          FB     
Indicators  Shares  ROE Shares  ROE
Quarter_end                        
2018-03-24     NaN  NaN    NaN  NaN
2018-03-25     NaN  NaN    NaN  NaN
2018-03-30     NaN  NaN    NaN  NaN
2018-03-31     1.0  2.0    3.0  4.0
2018-04-01     NaN  NaN    NaN  NaN

安装程序
您是否尝试过使用read\u csv的index\u col参数?()您是否尝试过使用read\u csv的index\u col参数?回答得好。很高兴知道
hdf
本机支持在
pandas
中读取/写入多个索引。回答得好。很高兴知道
hdf
本机支持在
pandas
中读取/写入多个索引。