从多索引 csv 文件创建多索引 pandas 数据框
Create a mulitindex pandas data frame from a multiindex csv file
我正在尝试从现有的 csv 文件创建一个多索引 pandas 数据框(最终是一个 csv 文件)。我很难遍历数据框,因为它包含超过 2 个维度。我该如何做到这一点?原始 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
stack
和 unstack
是重塑数据框的有用函数,前者将列索引转换为行索引,后者则相反,也是 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())
我正在尝试从现有的 csv 文件创建一个多索引 pandas 数据框(最终是一个 csv 文件)。我很难遍历数据框,因为它包含超过 2 个维度。我该如何做到这一点?原始 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
stack
和 unstack
是重塑数据框的有用函数,前者将列索引转换为行索引,后者则相反,也是 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())