如何在忽略索引的同时使用isin [英] How to use isin while ignoring index
本文介绍了如何在忽略索引的同时使用isin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试检查其他数据框中是否存在行.由于创建重复项存在问题,因此我没有加入/合并,然后需要过滤掉该重复项,这可能还会过滤掉我想要保留的实际重复项.
I'm trying to check if rows exist in another dataframe. I'm not joining/merging because of issues with it creating duplication, and then needing to filter out that duplication might also filter out actual duplication that I want to keep.
示例:
table1 = pd.DataFrame({'a':[1, 2, 5, 3, 4],
'b':['a', 'b', 'e', 'c', 'd']})
table2 = pd.DataFrame({'a':[1, 4, 3, 6, 2],
'b':['a', 'd', 'c', 'f', 'b']})
table1.isin(table2)
a b
0 True True
1 False False
2 False False
3 False False
4 False False
我希望所有这些都是True
,除了在索引2处,其中table2
中不存在行5 e
.
I would like all of these to be True
except at index 2 where row 5 e
doesn't exist in table2
.
推荐答案
IIUC
table1.stack().isin(table2.stack().values).unstack()
Out[207]:
a b
0 True True
1 True True
2 False False
3 True True
4 True True
如果检查行基
table1.astype(str).sum(1).isin(table2.astype(str).sum(1))
通过使用merge
table1.merge(table2.assign(vec=True),how='left').fillna(False)
Out[232]:
a b vec
0 1 a True
1 2 b True
2 5 e False
3 3 c True
4 4 d True
这篇关于如何在忽略索引的同时使用isin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文