w3resource

Pandas: Sort a MultiIndex of a DataFrame and on various index levels

Pandas Indexing: Exercise-16 with Solution

Write a Pandas program to sort a MultiIndex of a DataFrame. Also sort on various levels of index.

Sample Solution:

Python Code :

import pandas as pd 
import numpy as np
sales_arrays = [['sale1', 'sale1', 'sale3', 'sale3', 'sale2', 'sale2', 'sale4', 'sale4'],
          ['city1', 'city2', 'city1', 'city2', 'city1', 'city2', 'city1', 'city2']]
sales_tuples = list(zip(*sales_arrays))
sales_index = pd.MultiIndex.from_tuples(sales_tuples, names=['sale', 'city'])
print(sales_tuples)
print("\nConstruct a Dataframe using the said MultiIndex levels: ")
df = pd.DataFrame(np.random.randn(8, 5), index=sales_index)
print(df)
print("\nSort on MultiIndex DataFrame:")
df1 = df.sort_index()
print("\nSort on Index level=0 of the DataFrame:")
df2 = df.sort_index(level=0)
print(df2)
print("\nSort on Index level=1 of the DataFrame:")
df2 = df.sort_index(level=1)
print(df2)
print("\nPass a level name to sort the DataFrame:")
df3 = df.sort_index(level="city")
print(df3)  

Sample Output:

[('sale1', 'city1'), ('sale1', 'city2'), ('sale3', 'city1'), ('sale3', 'city2'), ('sale2', 'city1'), ('sale2', 'city2'), ('sale4', 'city1'), ('sale4', 'city2')]

Construct a Dataframe using the said MultiIndex levels: 
                    0         1         2         3         4
sale  city                                                   
sale1 city1 -0.089370  0.705290 -0.666095  1.123766  0.913882
      city2  1.191204  0.109838 -0.103562 -0.184452 -1.955661
sale3 city1  0.545379  0.463709  0.024852  0.986675  0.615907
      city2 -0.258394 -0.453250 -0.536596 -0.219055  0.430811
sale2 city1 -1.073173  0.555573 -0.112394 -0.100727 -0.241135
      city2 -0.876008  0.919629  0.296234 -0.507162 -1.813040
sale4 city1  0.515350  0.683498  0.405508  2.010065  0.170758
      city2  0.695778 -2.036874 -0.543257  0.601770 -0.539135

Sort on MultiIndex DataFrame:

Sort on Index level=0 of the DataFrame:
                    0         1         2         3         4
sale  city                                                   
sale1 city1 -0.089370  0.705290 -0.666095  1.123766  0.913882
      city2  1.191204  0.109838 -0.103562 -0.184452 -1.955661
sale2 city1 -1.073173  0.555573 -0.112394 -0.100727 -0.241135
      city2 -0.876008  0.919629  0.296234 -0.507162 -1.813040
sale3 city1  0.545379  0.463709  0.024852  0.986675  0.615907
      city2 -0.258394 -0.453250 -0.536596 -0.219055  0.430811
sale4 city1  0.515350  0.683498  0.405508  2.010065  0.170758
      city2  0.695778 -2.036874 -0.543257  0.601770 -0.539135

Sort on Index level=1 of the DataFrame:
                    0         1         2         3         4
sale  city                                                   
sale1 city1 -0.089370  0.705290 -0.666095  1.123766  0.913882
sale2 city1 -1.073173  0.555573 -0.112394 -0.100727 -0.241135
sale3 city1  0.545379  0.463709  0.024852  0.986675  0.615907
sale4 city1  0.515350  0.683498  0.405508  2.010065  0.170758
sale1 city2  1.191204  0.109838 -0.103562 -0.184452 -1.955661
sale2 city2 -0.876008  0.919629  0.296234 -0.507162 -1.813040
sale3 city2 -0.258394 -0.453250 -0.536596 -0.219055  0.430811
sale4 city2  0.695778 -2.036874 -0.543257  0.601770 -0.539135

Pass a level name to sort the DataFrame:
                    0         1         2         3         4
sale  city                                                   
sale1 city1 -0.089370  0.705290 -0.666095  1.123766  0.913882
sale2 city1 -1.073173  0.555573 -0.112394 -0.100727 -0.241135
sale3 city1  0.545379  0.463709  0.024852  0.986675  0.615907
sale4 city1  0.515350  0.683498  0.405508  2.010065  0.170758
sale1 city2  1.191204  0.109838 -0.103562 -0.184452 -1.955661
sale2 city2 -0.876008  0.919629  0.296234 -0.507162 -1.813040
sale3 city2 -0.258394 -0.453250 -0.536596 -0.219055  0.430811
sale4 city2  0.695778 -2.036874 -0.543257  0.601770 -0.539135

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to rename names of columns and specific labels of the Main Index of the MultiIndex dataframe.

Next: Write a Pandas program to extract elements in the given positional indices along an axis of a dataframe.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.