Pandas Python - 如何根据特定的标签列表重新排序二级行索引

Pandas Python - How to reorder second level row indexes according to a specific list of labels

如何根据特定的标签列表对二级行索引(“产品代码”)进行排序?

这是用于 运行 我的具有 2 级行索引的示例数据集的代码:

tdf = df.pivot_table(index=["Shop Outlet", "Product Code"],
                     columns="Year",
                     values=["Product Sales Vol","Unique Product"],
                     aggfunc= lambda x: len(x.unique()),
                     margins=True)
tdf

这是数据集结构的示例数据:

                                      Product Sales Vol         Unique Product  
Shop Outlet    Product Code          2019   2020   2021     2019   2020   2021
 USA                     OL            75     89    102        5      6      8   
                         P1            75     89    102        5      6      8
                         P2            75     89    102        5      6      8
                         P3            75     89    102        5      6      8
                         P4            75     89    102        5      6      8
                         P5            75     89    102        5      6      8
                         P6            75     89    102        5      6      8
                         PP            75     89    102        5      6      8
                         S1            75     89    102        5      6      8
                         S2            75     89    102        5      6      8
                         S3            75     89    102        5      6      8
 Canada                  OL            75     89    102        5      6      8   
                         P1            75     89    102        5      6      8
                         P2            75     89    102        5      6      8
                         P3            75     89    102        5      6      8
                         P4            75     89    102        5      6      8
                         P5            75     89    102        5      6      8
                         P6            75     89    102        5      6      8
                         PP            75     89    102        5      6      8
                         S1            75     89    102        5      6      8
                         S2            75     89    102        5      6      8
                         S3            75     89    102        5      6      8

我希望能够 i) 根据下面的标签列表按特定顺序对二级行索引(产品代码)重新排序,ii) 并在产品代码下添加“全部”类别对于每个商店出口。这将是数据集的示例:

product_codes = ["PP", "P1", "P2", "P3", "P4", "P5", "P6", "S1", "S2", "S3", "OL"]

                                      Product Sales Vol         Unique Product  
Shop Outlet    Product Code          2019   2020   2021     2019   2020   2021
 USA                     PP            75     89    102        5      6      8   
                         P1            75     89    102        5      6      8
                         P2            75     89    102        5      6      8
                         P3            75     89    102        5      6      8
                         P4            75     89    102        5      6      8
                         P5            75     89    102        5      6      8
                         P6            75     89    102        5      6      8
                         S1            75     89    102        5      6      8
                         S2            75     89    102        5      6      8
                         S3            75     89    102        5      6      8
                         OL            75     89    102        5      6      8
                        All           825    979   1122       55     66     88
 Canada                  PP            75     89    102        5      6      8   
                         P1            75     89    102        5      6      8
                         P2            75     89    102        5      6      8
                         P3            75     89    102        5      6      8
                         P4            75     89    102        5      6      8
                         P5            75     89    102        5      6      8
                         P6            75     89    102        5      6      8
                         S1            75     89    102        5      6      8
                         S2            75     89    102        5      6      8
                         S3            75     89    102        5      6      8
                         OL            75     89    102        5      6      8
                        All           825    979   1122       55     66     88

你可以尝试以下方法吗:

tdf.reindex(product_codes, level=1)