Examples of how to swap indexes in a multiple indexes dataframe with pandas:
Create a multiple indexes dataframe with pandas
Let's first create a multiple indexes dataframe with pandas using MultiIndex / advanced indexing,see also how to create a dataframe with multiple indexes with pandas
import pandas as pditerables = [["Land", "Ocean", "Snow/Ice"], ["Liquid", "Ice"]]index = pd.MultiIndex.from_product(iterables, names=["Id1", "Id2"])df = pd.DataFrame({'Count A': [12., 70., 30., 20., 17.0, 45.0], 'Count B': [12., 70., 30., 20., 17.0, 45.0]}, index=index)
returns
Count A Count BId1 Id2Land Liquid 12.0 12.0Ice 70.0 70.0Ocean Liquid 30.0 30.0Ice 20.0 20.0Snow/Ice Liquid 17.0 17.0Ice 45.0 45.0
Swap indexes of a dataframe
To swap id1 and id2, a solution is to use pandas.DataFrame.swaplevel
df = df.swaplevel()
returns then
Count A Count BId2 Id1Liquid Land 12.0 12.0Ice Land 70.0 70.0Liquid Ocean 30.0 30.0Ice Ocean 20.0 20.0Liquid Snow/Ice 17.0 17.0Ice Snow/Ice 45.0 45.0Note: to make the dataframe more easiest to read, a solution is to use [pandas.DataFrame.sort_index](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_index.html)df = df.sort_index()givesCount A Count BId2 Id1Ice Land 70.0 70.0Ocean 20.0 20.0Snow/Ice 45.0 45.0Liquid Land 12.0 12.0Ocean 30.0 30.0Snow/Ice 17.0 17.0
Another example with 3 levels of index
Another example with a dataframe with 3 indexes:
import pandas as pditerables = [["Land", "Ocean"], ["Liquid", "Ice"], ["Successful", "Unsuccessful"]]index = pd.MultiIndex.from_product(iterables, names=["Id1", "Id2", "Id3"])df = pd.DataFrame({'Count A': [12., 70., 30., 20., 17.0, 45.0, 42., 7.], 'Count B': [12., 70., 30., 20., 17.0, 45.0, 42., 7.]}, index=index)Count A Count BId1 Id2 Id3Land Liquid Successful 12.0 12.0Unsuccessful 70.0 70.0Ice Successful 30.0 30.0Unsuccessful 20.0 20.0Ocean Liquid Successful 17.0 17.0Unsuccessful 45.0 45.0Ice Successful 42.0 42.0Unsuccessful 7.0 7.0
Note: with more than 2 levels, by default swaplevel() will swap the last two levels (e.g. id2 and id3 here):
df = df.swaplevel()
returns
Count A Count BId1 Id3 Id2Land Successful Liquid 12.0 12.0Unsuccessful Liquid 70.0 70.0Successful Ice 30.0 30.0Unsuccessful Ice 20.0 20.0Ocean Successful Liquid 17.0 17.0Unsuccessful Liquid 45.0 45.0Successful Ice 42.0 42.0Unsuccessful Ice 7.0 7.0df = df.sort_index()givesCount A Count BId1 Id3 Id2Land Successful Ice 30.0 30.0Liquid 12.0 12.0Unsuccessful Ice 20.0 20.0Liquid 70.0 70.0Ocean Successful Ice 42.0 42.0Liquid 17.0 17.0Unsuccessful Ice 7.0 7.0Liquid 45.0 45.0
Choose wich indexes to swap
Let's re-create the dataframe:
import pandas as pditerables = [["Land", "Ocean"], ["Liquid", "Ice"], ["Successful", "Unsuccessful"]]index = pd.MultiIndex.from_product(iterables, names=["Id1", "Id2", "Id3"])df = pd.DataFrame({'Count A': [12., 70., 30., 20., 17.0, 45.0, 42., 7.], 'Count B': [12., 70., 30., 20., 17.0, 45.0, 42., 7.]}, index=index)Count A Count BId1 Id2 Id3Land Liquid Successful 12.0 12.0Unsuccessful 70.0 70.0Ice Successful 30.0 30.0Unsuccessful 20.0 20.0Ocean Liquid Successful 17.0 17.0Unsuccessful 45.0 45.0Ice Successful 42.0 42.0Unsuccessful 7.0 7.0
To swap id1 and id2, a solution is to do
df = df.swaplevel(0,1)
returns then
Count A Count BId2 Id1 Id3Liquid Land Successful 12.0 12.0Unsuccessful 70.0 70.0Ice Land Successful 30.0 30.0Unsuccessful 20.0 20.0Liquid Ocean Successful 17.0 17.0Unsuccessful 45.0 45.0Ice Ocean Successful 42.0 42.0Unsuccessful 7.0 7.0
and
df = df.sort_index()
will give
Count A Count BId2 Id1 Id3Ice Land Successful 30.0 30.0Unsuccessful 20.0 20.0Ocean Successful 42.0 42.0Unsuccessful 7.0 7.0Liquid Land Successful 12.0 12.0Unsuccessful 70.0 70.0Ocean Successful 17.0 17.0Unsuccessful 45.0 45.0
