【发布时间】:2021-12-18 20:07:52
【问题描述】:
我有一个像这样的多级索引数据框:
indx = [('location', 'a'), ('location', 'b'), ('location', 'c'), ('location2', 'a'), ('location2', 'b'), ('location2', 'c')]
indx = pd.MultiIndex.from_tuples(indx)
col = ['S1','S2','S3']
df = pd.DataFrame(np.random.randn(6, 3), index=indx, columns=col)
df
S1 S2 S3
location a -0.453549 -0.079797 0.581657
b -0.458573 -0.732625 -2.277674
c 0.874403 0.459590 -1.220271
location2 a -1.418821 0.847556 0.665808
b 1.321825 -0.150274 0.507685
c 0.894865 -0.502577 2.460591
我正在尝试将索引的第一级移动为多级列的第一级,如下所示:
header = [np.array(['location','location','location','location2','location2','location2']),
np.array(['S1','S2','S3','S1','S2','S3'])]
df = pd.DataFrame(np.random.randn(3, 6), index=['a','b','c'], columns = header )
df
location location2
S1 S2 S3 S1 S2 S3
a 1.225889 -0.547848 -0.413482 -1.038456 -1.055484 -0.143620
b -1.278012 0.403767 -0.298525 0.198157 0.327973 -0.088555
c -1.152128 0.209956 0.939142 0.458412 1.207760 -0.091029
有什么想法吗?
【问题讨论】:
标签: python pandas indexing pivot multi-level