我有一个像这样的MultiIndexed数据框:
foo
c b a
p 6 1 3.0
q 7 2 2.3
r 8 3 1.0
s 9 4 100.0
我可以使用drop
使用第一个n
MultiIndex级别删除多行,如下所示:
>>> x.drop([('p', 6), ('r',8)])
foo
c b a
q 7 2 2.3
s 9 4 100.0
我也可以从单一级别drop
:
>>> x.drop([1, 2], level='a')
foo
c b a
r 8 3 1.0
s 9 4 100.0
但我似乎无法在多个级别(第一个n
除外)执行此操作:
>>> x.drop([(8, 3), (9, 4)], level=['b', 'a'])
Traceback (most recent call last):
File "<ipython-input-156-a650ded10561>", line 1, in <module>
x.drop([(8, 3), (9, 4)], level=['b', 'a'])
File "/usr/lib/python2.7/dist-packages/pandas/core/generic.py", line 1399, in drop
new_axis = axis.drop(labels, level=level)
File "/usr/lib/python2.7/dist-packages/pandas/core/index.py", line 2718, in drop
return self._drop_from_level(labels, level)
File "/usr/lib/python2.7/dist-packages/pandas/core/index.py", line 2744, in _drop_from_level
i = self._get_level_number(level)
File "/usr/lib/python2.7/dist-packages/pandas/core/index.py", line 2199, in _get_level_number
raise KeyError('Level %s not found' % str(level))
KeyError: "Level ['b', 'a'] not found"
这看起来很奇怪,因为xs
接受一个级别列表,例如:
>>> df.xs(('baz', 2), level=[0, 'third'])
A B C D
second
three 5 3 5 3
那么如何从数据框中删除[(8, 3), (9, 4)]
(即第3行和第4行)?
答案 0 :(得分:2)
此功能尚不存在,请参阅此问题:https://github.com/pydata/pandas/pull/6599
但是你可以这样做。
In [19]: mask = df.index.get_level_values
In [20]: df.loc[~(mask('b').isin([8,9]) & mask('a').isin([3,4]))]
Out[20]:
foo
c b a
p 6 1 3.0
q 7 2 2.3