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
中读取/写入多个索引。