Dominique Paul
Dominique Paul

Reputation: 1751

Pandas re-indexing with Multiindex on Index AND Columns

I have a long dataframe with these columns in this format:

id  gender  size    region_a_count  region_b_count  item_group
 x   m       x            x                x         x
 x   f       x            x                x         x
 x   f       x            x                x         x
 x   m       x            x                x         x
 x   f       x            x                x         x
 x   m       x            x                x         x   

Each combination of gender, size,item_group and region is unique. So there is no need to perform an aggregation.

I want to reindex the index and columns to this type of output, with the cell values staying the same

                    region_a         region_b        region_c   
                    m        f       m        f      m         f
      (index of size)
item_group  1                       
            2                       
            3                       
item_group  1                       
            2                       
            3                       

That is, with index as well as columns having a multiindex. I was able to perform the part for the index by calling:

df.groupby(["item_group","size"])

But the column problem still remains.

How can I create the column multiindex from the existing dataframe?

Upvotes: 1

Views: 371

Answers (1)

jezrael
jezrael

Reputation: 862406

I believe you need set_index with unstack:

df1 = df.set_index(["item_group","size", "id","gender"]).unstack()

Upvotes: 1

Related Questions