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

PythonServer Side ProgrammingProgramming

To drop multiple levels from a multi-level column index, use the columns.droplevel() repeatedly. 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)

We deleted a level at 0 index. After deleting, the level 1 is now level 0. To delete another level, just use the above again i.e.

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

Following is the code

Example

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 = ['one', 'two']
print"DataFrame...\n",dataFrame

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

print"\nDropping another 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
one      two
car      valueA   0.425077   0.020606   1.148156
         valueB  -1.720355   0.502863   1.184753
         valueC   0.373106   1.300935  -0.128404
bike     valueA  -0.648708   0.944725   0.593327
         valueB  -0.613921  -0.238730  -0.218448
         valueC   0.313042  -0.628065   0.910935
truck    valueA   0.286377   0.478067  -1.000645
         valueB   1.151793  -0.171433  -0.612346
         valueC  -1.358061   0.735075   0.092700

Dropping a level...

Updated DataFrame..
                     Col 1      Col 2      Col 3
                     Col 1      Col 2      Col 3
one      two
car      valueA   0.425077   0.020606   1.148156
         valueB  -1.720355   0.502863   1.184753
         valueC   0.373106   1.300935  -0.128404
bike     valueA  -0.648708   0.944725   0.593327
         valueB  -0.613921  -0.238730  -0.218448
         valueC   0.313042  -0.628065   0.910935
truck    valueA   0.286377   0.478067  -1.000645
         valueB   1.151793  -0.171433  -0.612346
         valueC  -1.358061   0.735075   0.092700

Dropping another level...

Updated DataFrame..
                     Col 1     Col 2     Col 3
one      two
car      valueA   0.425077  0.020606  1.148156
         valueB  -1.720355  0.502863  1.184753
         valueC   0.373106  1.300935 -0.128404
bike     valueA  -0.648708  0.944725  0.593327
         valueB  -0.613921 -0.238730 -0.218448
         valueC   0.313042 -0.628065  0.910935
truck    valueA   0.286377  0.478067 -1.000645
         valueB   1.151793 -0.171433 -0.612346
         valueC  -1.358061  0.735075  0.092700
raja
Published on 13-Sep-2021 09:15:08
Advertisements