Search code examples
pythonpandasmulti-index

Drop Single Row from MultiIndex Dataffame


Imagine that I have the following DataFrame:

                0         1         2         3
bar one -0.424972  0.567020  0.276232 -1.087401
    two -0.673690  0.113648 -1.478427  0.524988
baz one  0.404705  0.577046 -1.715002 -1.039268
    two -0.370647 -1.157892 -1.344312  0.844885
foo one  1.075770 -0.109050  1.643563 -1.469388
    two  0.357021 -0.674600 -1.776904 -0.968914
qux one -1.294524  0.413738  0.276662 -0.472035
    two -0.013960 -0.362543 -0.006154 -0.923061

And I only want to drop the row indexed by ["foo", "two"]. Is there a simple way to do this?

Using drop I can drop both the records indexed under foo at level 0, or all records indexed by two at level 1. But I only want to drop the one.


Solution

  • You can drop with tuple

    df.drop(('foo','two'))
    Out[381]: 
                    0         1         2         3
    
    bar one -0.424972  0.567020  0.276232 -1.087401
        two -0.673690  0.113648 -1.478427  0.524988
    baz one  0.404705  0.577046 -1.715002 -1.039268
        two -0.370647 -1.157892 -1.344312  0.844885
    foo one  1.075770 -0.109050  1.643563 -1.469388
    qux one -1.294524  0.413738  0.276662 -0.472035
        two -0.013960 -0.362543 -0.006154 -0.923061