Exemples de comment échanger les indices d'une dataframe à plusieurs indices avec pandas :
Créer une datafrane avec plusieurs indices
Pour créer une datafrane avec plusieurs indices avec pandas, une solution est d'utiliser MultiIndex / advanced indexing, voir aussi how to create a dataframe with multiple indexes with pandas
import pandas as pd
iterables = [["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)
donne
Count A Count B
Id1 Id2
Land Liquid 12.0 12.0
Ice 70.0 70.0
Ocean Liquid 30.0 30.0
Ice 20.0 20.0
Snow/Ice Liquid 17.0 17.0
Ice 45.0 45.0
Échanger les indices d'une dataframe
Pour échanger id1 et id2, une solution est d'utiliser pandas.DataFrame.swaplevel
df = df.swaplevel()
donne alors
Count A Count B
Id2 Id1
Liquid Land 12.0 12.0
Ice Land 70.0 70.0
Liquid Ocean 30.0 30.0
Ice Ocean 20.0 20.0
Liquid Snow/Ice 17.0 17.0
Ice Snow/Ice 45.0 45.0
Note: pour rendre la dataframe plus facile à lire, une solution consiste à utiliser [pandas.DataFrame.sort_index](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_index.html)
df = df.sort_index()
donne alors
Count A Count B
Id2 Id1
Ice Land 70.0 70.0
Ocean 20.0 20.0
Snow/Ice 45.0 45.0
Liquid Land 12.0 12.0
Ocean 30.0 30.0
Snow/Ice 17.0 17.0
Un autre exemple avec 3 indices
Autre exemple avec un dataframe à 3 indices
import pandas as pd
iterables = [["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)
donne
Count A Count B
Id1 Id2 Id3
Land Liquid Successful 12.0 12.0
Unsuccessful 70.0 70.0
Ice Successful 30.0 30.0
Unsuccessful 20.0 20.0
Ocean Liquid Successful 17.0 17.0
Unsuccessful 45.0 45.0
Ice Successful 42.0 42.0
Unsuccessful 7.0 7.0
Note: avec plus de 2 niveaux d'indices, par défaut swaplevel() permutera les deux derniers niveaux (par exemple id2 et id3 ici):
df = df.swaplevel()
donne
Count A Count B
Id1 Id3 Id2
Land Successful Liquid 12.0 12.0
Unsuccessful Liquid 70.0 70.0
Successful Ice 30.0 30.0
Unsuccessful Ice 20.0 20.0
Ocean Successful Liquid 17.0 17.0
Unsuccessful Liquid 45.0 45.0
Successful Ice 42.0 42.0
Unsuccessful Ice 7.0 7.0
df = df.sort_index()
donne
Count A Count B
Id1 Id3 Id2
Land Successful Ice 30.0 30.0
Liquid 12.0 12.0
Unsuccessful Ice 20.0 20.0
Liquid 70.0 70.0
Ocean Successful Ice 42.0 42.0
Liquid 17.0 17.0
Unsuccessful Ice 7.0 7.0
Liquid 45.0 45.0
Choisissez les indices à échanger
Recréons la dataframe :
import pandas as pd
iterables = [["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)
donne
Count A Count B
Id1 Id2 Id3
Land Liquid Successful 12.0 12.0
Unsuccessful 70.0 70.0
Ice Successful 30.0 30.0
Unsuccessful 20.0 20.0
Ocean Liquid Successful 17.0 17.0
Unsuccessful 45.0 45.0
Ice Successful 42.0 42.0
Unsuccessful 7.0 7.0
Pour échanger id1 et id2, une solution est de faire
df = df.swaplevel(0,1)
donne alors
Count A Count B
Id2 Id1 Id3
Liquid Land Successful 12.0 12.0
Unsuccessful 70.0 70.0
Ice Land Successful 30.0 30.0
Unsuccessful 20.0 20.0
Liquid Ocean Successful 17.0 17.0
Unsuccessful 45.0 45.0
Ice Ocean Successful 42.0 42.0
Unsuccessful 7.0 7.0
et
df = df.sort_index()
donnera
Count A Count B
Id2 Id1 Id3
Ice Land Successful 30.0 30.0
Unsuccessful 20.0 20.0
Ocean Successful 42.0 42.0
Unsuccessful 7.0 7.0
Liquid Land Successful 12.0 12.0
Unsuccessful 70.0 70.0
Ocean Successful 17.0 17.0
Unsuccessful 45.0 45.0