2016-12-16 215 views
1

我有一個熊貓DataFrameMultiIndex在列(可以說3級):如何重新索引Pandas Dataframe的列索引MultiIndex?

MultiIndex(levels=[['BA-10.0', 'BA-2.5', ..., 'p'], ['41B004', '41B005', ..., 'T1M003', 'T1M011'], [25, 26, ..., 276, 277]], 
      labels=[[0, 0, 0, ..., 18, 19, 19], [4, 5, 6,..., 14, 12, 13], [24, 33, 47, ..., 114, 107, 113]], 
      names=['measurandkey', 'sitekey', 'channelid']) 

當我通過DataFrame第一級和產量的子集ITER:

def cluster(df): 
    for key in df.columns.levels[0]: 
     yield df[key] 

for subdf in cluster(df): 
    print(subdf.columns) 

列的索引也已經失去了它的第一級,但MultiIndex仍包含對子級中的所有其他鍵的引用,即使它們在子集中缺失。

MultiIndex(levels=[['41B004', '41B005', '41B006', '41B008', '41B011', '41MEU1', '41N043', '41R001', '41R002', '41R012', '41WOL1', '41WOL2', 'T1M001', 'T1M003', 'T1M011'], [25, 26, 27, 28, 30, 31, 32, 3, .... 
      labels=[[4, 5, 6, 7, 9, 10], [24, 33, 47, 61, 83, 98]], 
      names=['sitekey', 'channelid']) 

我怎麼能強迫subdf有它的列多指標僅是存在的密鑰更新?

+0

'df.reset_index()'? – MMF

+0

這似乎並沒有竅門 – jlandercy

回答

1
def cluster(df): 
    for key in df.columns.levels[0]: 
     d = df[key] 
     d.columns = pd.MultiIndex.from_tuples(d.columns.to_series()) 
     yield d 
+0

工作謝謝你 – jlandercy

相關問題