Python – Drop a level from a multi-level column index in Pandas dataframe

PythonServer Side ProgrammingProgramming

To drop a level from a multi-level column index, use the columns.droplevel(). We have used the Multiindex.from_tuples() is used to create indexes column-wise.

At first, create indexes column-wise −

items = pd.MultiIndex.from_tuples([("Col 1", "Col 1", "Col 1"),("Col 2", "Col 2", "Col 2"),("Col 3", "Col 3", "Col 3")])

Next, create a multiindex array and form a multiindex dataframe

arr = [np.array(['car', 'car', 'car','bike','bike', 'bike', 'truck', 'truck', 'truck']),

   np.array(['valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC'])]

# forming multiindex dataframe
dataFrame = pd.DataFrame(np.random.randn(9, 3), index=arr,columns=items)

Label the index −

dataFrame.index.names = ['level 0', 'level 1']

Drop a level at index 0 −

dataFrame.columns = dataFrame.columns.droplevel(0)

Example

Following is the code

import numpy as np
import pandas as pd

items = pd.MultiIndex.from_tuples([("Col 1", "Col 1", "Col 1"),("Col 2", "Col 2", "Col 2"),("Col 3", "Col 3", "Col 3")])

# multiindex array
arr = [np.array(['car', 'car', 'car','bike','bike', 'bike', 'truck', 'truck', 'truck']),

np.array(['valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC'])]

# forming multiindex dataframe
dataFrame = pd.DataFrame(np.random.randn(9, 3), index=arr,columns=items)

# labelling index
dataFrame.index.names = ['level 0', 'level 1']
print"DataFrame...\n",dataFrame

print"\nDropping a level...\n";
dataFrame.columns = dataFrame.columns.droplevel(0)

print"Updated DataFrame..\n",dataFrame

Output

This will produce the following output

DataFrame...
                     Col 1      Col 2      Col 3
                     Col 1      Col 2      Col 3
                     Col 1      Col 2      Col 3
level 0  level 1
car      valueA   1.691127   0.315145  -0.695925
         valueB  -2.077182  -2.027643  -0.523965
         valueC   1.021402  -0.384421   0.640215
bike     valueA  -2.271217   0.197185   0.304847
         valueB   0.119615  -0.520491  -0.746547
         valueC   1.856888  -0.491540  -1.754604
truck    valueA   0.829854  -0.204102  -1.130511
         valueB   0.310692   0.119087  -0.244919
         valueC  -0.245934  -2.141639  -1.298278

Dropping a level...

Updated DataFrame..
                      Col 1      Col 2       Col 3
                      Col 1      Col 2       Col 3
level 0  level 1
car      valueA 1.691127   0.315145   -0.695925
         valueB   -2.077182  -2.027643   -0.523965
         valueC    1.021402  -0.384421    0.640215
bike     valueA   -2.271217   0.197185    0.304847
         valueB    0.119615  -0.520491   -0.746547
         valueC    1.856888  -0.491540   -1.754604
truck    valueA    0.829854  -0.204102   -1.130511
         valueB    0.310692   0.119087   -0.244919
         valueC   -0.245934  -2.141639   -1.298278
raja
Published on 13-Sep-2021 08:55:25
Advertisements