Home > database >  Drop even levels from multi-index dataset
Drop even levels from multi-index dataset

Time:07-03

Here is sample dataset:

                          |Count|
|-------------------------|     |
|Name |ID_1  |Level |ID_2 |     |
|-----|------|------|-----|-----|
|Kate |91978 |Junior|3    |13   |
|Lucy |47992 |Junior|3    |11   |
|John |37005 |Middle|2    |8    |
|Peter|42235 |Senior|1    |21   |

Let's say ['Name', 'ID_1', 'Level','ID_2'] are multi-index names

I want to drop all even multi-index names, meaning get rid of ['ID_1',ID_2'] So that end result looks like this:

              |Count|
|-------------|     |
|Name  |Level |     |
|------|------|-----|
|Kate  |Junior|13   |
|Lucy  |Junior|11   |
|John  |Middle|8    |
|Peter |Senior|21   |

I found this method: df.index = df.index.droplevel(1)

But the thing is the real dataset is too big, and dropping each second column manually is not an option. How to drop all even columns at once?

CodePudding user response:

You can pass a list of index levels to DataFrame.droplevel.

For instance, given the following DataFrame

import pandas as pd

df = (
    pd.DataFrame(np.random.randint(5, size=(5,5)), 
                 columns=list('abcde'))
      .set_index(list('abcd'))
)
>>> df

         e
a b c d   
0 4 2 0  2
3 2 3 1  1
4 2 2 3  4
0 0 1 4  2
  4 3 4  4

You can do something like

res = df.droplevel(list(range(1, len(df.index.names), 2)))
>>> res

     e
a c   
0 2  2
3 3  1
4 2  4
0 1  2
  3  4

CodePudding user response:

Df

import pandas.util.testing
df =pd.DataFrame(np.random.rand(4,8),columns=list('abcdfrtl'))

drop even indices; -

df.droplevel(df.index.names[1::2])

drop even columns; - Or use the loc accessor

new=df.iloc[:,0::2]



     a         c         f         t
0  0.069244  0.747373  0.217126  0.779653
1  0.303143  0.288040  0.234507  0.923503
2  0.195276  0.131842  0.447319  0.511451
3  0.821173  0.776493  0.827540  0.679356
  • Related