[英]Drop multi-indexed rows of a DataFrame based on 'AND' condition between levels
I want to be able to drop rows from a multi-indexed dataframe object using multiple level criteria (with a logical AND joining the criteria). 我希望能够使用多个级别标准从多索引数据框对象中删除行(使用逻辑AND加入条件)。
Consider the pandas dataframe object given by: 考虑由下面给出的pandas dataframe对象:
import pandas as pd
df = pd.DataFrame(data = [[1,'x'],[2,'x'],[1,'y'],[2,'y']],
index=pd.MultiIndex(levels=[['A','B'],['a','b']],
labels=[[0,1,0,1],[0,1,1,0]],
names=['idx0','idx1']))
print(df)
outputs: print(df)
输出:
0 1
idx0 idx1
A a 1 x
B b 2 x
A b 1 y
B a 2 y
I wish to eliminate the row where 'idx0'=='A'
and 'idx1'=='a'
, so the end result is: 我想消除
'idx0'=='A'
和 'idx1'=='a'
,所以最终的结果是:
0 1
idx0 idx1
B b 2 x
a 2 y
A b 1 y
It seems to me as if this cannot be done with the df.drop()
method. 在我看来,似乎无法使用
df.drop()
方法完成此操作。 A 'roundabout' way which gives the correct result is to do: 一种给出正确结果的“回旋”方式是:
df = pd.concat([df.drop(labels='A',level=0),df.drop(labels='a',level=1)])
df = df.drop_duplicates()
But I figure that there has to be a better way... 但我认为必须有更好的方法......
To address your question regarding .drop()
- just pass the MultiIndex
labels as tuple
: 要解决有关
.drop()
- 只需将MultiIndex
标签作为tuple
传递:
df.drop(('A', 'a'))
0 1
idx0 idx1
B b 2 x
A b 1 y
B a 2 y
You could use isin
method for index and take opposite to what are you selecting with ~
: 您可以将
isin
方法用于索引,并使用与您选择的内容相反~
:
In [85]: df.index.isin([('A','a')])
Out[85]: array([ True, False, False, False], dtype=bool)
In [86]: df[~df.index.isin([('A','a')])]
Out[86]:
0 1
idx0 idx1
B b 2 x
A b 1 y
B a 2 y
Timing: 定时:
In [95]: %timeit df.drop(('A','a'))
1000 loops, best of 3: 1.33 ms per loop
In [96]: %timeit df[~df.index.isin([('A','a')])]
1000 loops, best of 3: 457 us per loop
So drop is almost 3x times slower then with isin
solution. 所以使用
isin
溶液,下降几乎要慢3倍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.