Skip to content

pandas dataframe.drop(col,axis=1) does not drop column from column.levels in multiindex dataframe #12822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kdebrab opened this issue Apr 7, 2016 · 3 comments

Comments

@kdebrab
Copy link
Contributor

kdebrab commented Apr 7, 2016

bug in pandas version 0.18.0 (long time ago seemingly same issue has been reported on StackOverflow):

In[1]: import pandas as pd
  ...: import numpy as np
  ...: iterables = [['bar', 'baz', 'foo', 'qux'], ['one', 'two']]
  ...: index = pd.MultiIndex.from_product(iterables, names=['first', 'second'])
  ...: df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
  ...: df2 = df.drop('bar', axis=1)

At first sight, bar seems dropped from the dataframe:

In[2]: df2
Out[2]: 
first        baz                 foo                 qux          
second       one       two       one       two       one       two
A      -0.625880  0.154829 -0.416494 -1.423776  0.942012 -1.495733
B      -0.704217  0.132563  1.382712 -0.123216  1.773185  1.530121
C       0.925705 -0.702249  0.113887 -0.307644  0.369242 -0.798391

Though not completely, as 'bar' is still listed in the columns:

In[3]: df2.columns
Out[3]: 
MultiIndex(levels=[[u'bar', u'baz', u'foo', u'qux'], [u'one', u'two']],
           labels=[[1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1]],
           names=[u'first', u'second'])
@kdebrab
Copy link
Contributor Author

kdebrab commented Apr 7, 2016

I discovered issue #3686 just now...

@jreback
Copy link
Contributor

jreback commented Apr 7, 2016

dupe of #2770 not a bug. you are pearing into the implementation.

@deltaoui
Copy link

@kdebrab have figured any workaround to this issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants