2017-02-04 64 views
0

考虑以下多级数据帧删除数据帧的行基于列的值

import numpy as np 
import pandas as pd 
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], 
    ...:   ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] 
tuples = list(zip(*arrays)) 
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second']) 
s = pd.DataFrame(np.random.randn(8, 4), index=arrays) 
s 

可以说,我想删除整个一行index_0酒吧和INDEX_1 2

我怎么可能这样做?

回答

1

您可以使用drop method

In [26]: s.drop(('bar','two'), axis=0) 
Out[26]: 
       0   1   2   3 
bar one -0.450943 -1.615345 -0.862521 1.042495 
baz one 1.200944 0.617102 -0.439342 -0.296142 
    two -0.879343 -1.055805 0.682381 2.625398 
foo one 0.191370 -0.212905 -0.415360 -1.437934 
    two 0.458979 1.072584 0.485136 1.498859 
qux one -2.137894 -0.872023 -0.382530 -0.550116 
    two -1.490523 -2.999998 0.290653 -0.848422 

axis=0是没有必要的(这是缺省的),但我包括它只是要明确,我们是下跌行,而不是列。


如果你本来想删除多行,比方说,无论是 ('bar','two')('baz','one')行,那么你可以使用isin生成一个布尔面膜:

In [55]: s.index.isin((('bar','two'),('baz','one'))) 
Out[55]: array([False, True, True, False, False, False, False, False], dtype=bool) 

,然后使用s.loc选择行:

In [56]: s.loc[~s.index.isin((('bar','two'),('baz','one')))] 
Out[56]: 
       0   1   2   3 
bar one -0.450943 -1.615345 -0.862521 1.042495 
baz two -0.879343 -1.055805 0.682381 2.625398 
foo one 0.191370 -0.212905 -0.415360 -1.437934 
    two 0.458979 1.072584 0.485136 1.498859 
qux one -2.137894 -0.872023 -0.382530 -0.550116 
    two -1.490523 -2.999998 0.290653 -0.848422 

~反转面具小号o我们保留布尔掩码为False的行。