import pandas as pd
temp1 = pd.DataFrame(index=arange(10), columns=['a','b'])
temp1['a'] = [1,2,2,3,3,4,4,4,9,11]
temp1['b'] = 'B'
temp2 = pd.DataFrame(index=arange(10), columns=['a','b'])
temp2['a'] = [1,2,3,4,5,6,7,8,9,10]
temp2['b'] = 'B'
正如上面的脚本一样,我想从temp1
中拾取列a
没有在temp2
中看到的行。我可以在R中使用%in%
轻松地完成它,我如何在熊猫上这样做呢?
更新01
输出应为一行,其中列a
为11
,列b
为B
。
发布于 2014-07-29 09:16:31
可以使用isin
执行布尔索引:
isin
将生成一个布尔索引:
In [95]:
temp1.a.isin(temp2.a)
Out[95]:
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 False
Name: a, dtype: bool
然后,可以在最后的输出中使用它作为掩码:
In [94]:
# note the ~ this negates the result so equivalent of NOT
temp1[~temp1.a.isin(temp2.a)]
Out[94]:
a b
9 11 B
发布于 2014-07-29 09:16:26
您可以使用isin
获取所看到的索引,然后否定布尔索引:
temp1[~temp1.a.isin(temp2.a)]
https://stackoverflow.com/questions/25011761
复制相似问题