读取 python 中的多索引数据帧以保留格式
Reading Multi-index data frames in python to retain format
我有一个多索引数据框。
当我加载数据时
df = pd.read_excel(path + 'doc.xlsx', sheet_name='sheet1', index_col=[0,1])
看起来像这样
Index Index A B C D
0 AA 0.1 0.2 0.4 0.4
0 AB 0.0 -0.5 0.3 0.9
1 BB 0.4 0.6 1.2 0.8
1 BC 0.4 0.4 0.1 0.7
我希望它看起来像这样
Index Index A B C D
0 AA 0.1 0.2 0.4 0.4
AB 0.0 -0.5 0.3 0.9
1 BB 0.4 0.6 1.2 0.8
BC 0.4 0.4 0.1 0.7
如何通过不同方式读取此文件或合并第一个索引列,使其看起来像上面的 table。
我会尝试:
df = pd.read_excel(path + 'doc.xlsx', sheet_name='sheet1', index_col=[1,2])
如果仍然失败,我会这样做:
df = pd.read_excel(path + 'doc.xlsx', sheet_name='sheet1')
# rename columns beforehand if desired
df.set_index(['Index', 'Index.1'], inplace=True)
所以我的一些包已经过时了,我很快就开始正常工作了。谢谢大家的意见。
我有一个多索引数据框。
当我加载数据时
df = pd.read_excel(path + 'doc.xlsx', sheet_name='sheet1', index_col=[0,1])
看起来像这样
Index Index A B C D
0 AA 0.1 0.2 0.4 0.4
0 AB 0.0 -0.5 0.3 0.9
1 BB 0.4 0.6 1.2 0.8
1 BC 0.4 0.4 0.1 0.7
我希望它看起来像这样
Index Index A B C D
0 AA 0.1 0.2 0.4 0.4
AB 0.0 -0.5 0.3 0.9
1 BB 0.4 0.6 1.2 0.8
BC 0.4 0.4 0.1 0.7
如何通过不同方式读取此文件或合并第一个索引列,使其看起来像上面的 table。
我会尝试:
df = pd.read_excel(path + 'doc.xlsx', sheet_name='sheet1', index_col=[1,2])
如果仍然失败,我会这样做:
df = pd.read_excel(path + 'doc.xlsx', sheet_name='sheet1')
# rename columns beforehand if desired
df.set_index(['Index', 'Index.1'], inplace=True)
所以我的一些包已经过时了,我很快就开始正常工作了。谢谢大家的意见。