2016-12-15 132 views
1

我想從現有的csv文件創建一個多索引熊貓數據框(最終是一個csv文件)。由於它包含2個以上的維度,因此我很難在數據框上進行迭代。我該如何做到這一點?原來的CSV文件如下所示:從多索引csv文件創建一個多重索引熊貓數據框

"Products" "Technologies" Region1 Region2 Region3 
Prod1  Tech1   16  0  12 
Prod2  Tech2   0  12  22 
Prod3  Tech3   22  0  36 

而且我希望創造一個CSV文件,該文件是這樣的:

"Technologies" "Regions" Prod1 Prod2 Prod3 
Tech1   Region1  16  0  0 
Tech1   Region2  0  0  0 
Tech1   Region3  12  0  0 
Tech2   Region1  0  0  0 
Tech2   Region2  0  12  0 
Tech2   Region3  0  22  0 
Tech3   Region1  0  0  22 
Tech3   Region2  0  0  0 
Tech3   Region3  0  0  36 

回答

0

stackunstack是重塑數據幀比較實用的功能,前者變換的列索引到行索引和後者則正好相反,也check this tutorial

# transform regions to a separate column 
(df.set_index(["Products", "Technologies"]).stack() 

# rename the Region column and remove the name for Products column as it will be unstacked 
    .rename_axis(("", "Technologies", "Regions")) 

# unstack the product column to headers and reset the index to be columns 
    .unstack(level=0, fill_value=0).reset_index()) 

enter image description here

+0

謝謝Psidom! –