简体   繁体   English

从multiindex csv文件创建mulitindex pandas数据框

[英]Create a mulitindex pandas data frame from a multiindex csv file

I am trying to create a multi-index pandas data frame (ultimately a csv file) from an existing csv file. 我正在尝试从现有的csv文件创建多索引熊猫数据框(最终是一个csv文件)。 I am having a hard time iterating over data frame because it contains more than 2 dimensions. 我很难遍历数据帧,因为它包含两个以上的维。 How do I accomplish this? 我该如何完成? The original csv file looks like the following: 原始的csv文件如下所示:

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

And I am looking to create a csv file which looks like this: 我正在寻找创建一个如下所示的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

stack and unstack are helpful functions for reshaping data frame, the former transform the column index to row index and the latter does the opposite, also check this tutorial : stackunstack是重塑数据帧的有用功能,前者将列索引转换为行索引,而后者则相反,也可以查看此教程

# 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())

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM