Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping a single (sub-) column from a MultiIndex

I have the following dataframe df

    col1        col2        col3
    a     b     a     b     a     b       
1   ...
2
3

and just cannot figure out how to drop only a single 'sublevel', e.g. df.col1.a

I can df.col1.drop('a', axis=1), but reassigning it like df.col1=df.col1.drop('a', axis=1) fails.

The logical structure df.colums I understand, but how should I be modifying it?

like image 472
user3418530 Avatar asked Mar 14 '14 05:03

user3418530


1 Answers

Drop is a very flexible method, and there are quite a few ways to use it:

In [11]: mi = pd.MultiIndex.from_product([['col1', 'col2', 'col3'], ['a', 'b']])

In [12]: df = pd.DataFrame(1, index=[0], columns=mi)

In [13]: df
Out[13]:
   col1     col2     col3
      a  b     a  b     a  b
0     1  1     1  1     1  1

Drop a single column using a tuple:

In [14]: df.drop(('col1', 'a'), axis=1)
Out[14]:
   col1  col2     col3
      b     a  b     a  b
0     1     1  1     1  1

or a list using a list of tuples:

In [15]: df.drop([('col1', 'a'), ('col2', 'b')], axis=1)
Out[15]:
   col1  col2  col3
      b     a     a  b
0     1     1     1  1

or across a level, e.g. all as:

In [16]: df.drop('a', level=1, axis=1)
Out[16]:
   col1  col2  col3
      b     b     b
0     1     1     1

In 0.14, you'll also be able to pass regex of what to drop...

There's also a way to drop the entire level of a index/column:

In [21]: df.columns.droplevel(1)
Out[21]: Index([u'col1', u'col1', u'col2', u'col2', u'col3', u'col3'], dtype='object')
like image 177
Andy Hayden Avatar answered Sep 21 '22 01:09

Andy Hayden