我有两个数据帧 -
df1_data = {'sym1' :{0:'abc a01',1:'pqr q02',2:'xyz y03',3:'mno o12',4:'lmn l45'}}
df1 = pd.DataFrame(df1_data)
print df1
df2_data = {'sym2' :{0:'abc a01',1:'xxx p0',2:'xyz y03',3:'mno o12',4:'lmn l45',5:'rrr r1',6:'kkk k3'}}
df2 = pd.DataFrame(df2_data)
print df2
输出 -
sym1
0 abc a01
1 pqr q02
2 xyz y03
3 mno o12
4 lmn l45
sym2
0 abc a01
1 xxx p0
2 xyz y03
3 mno o12
4 lmn l45
5 rrr r1
6 kkk k3
我想在df2 dataframes sym1列中检查sym2列值是否可用。如果sym2列中的符号不可用,那么我想要在sym1列中不可用的符号列表。如果所有符号都可用,则列表必须为空。
预期结果 -
list -> ['xxx p0','rrr r1','kkk k3']
答案 0 :(得分:4)
您可以将boolean indexing
与isin
一起使用,然后按ix
选择并转换为list
tolist
:
print (~df2.sym2.isin(df1.sym1))
0 False
1 True
2 False
3 False
4 False
5 True
6 True
Name: sym2, dtype: bool
print (df2.ix[~df2.sym2.isin(df1.sym1), 'sym2'])
1 xxx p0
5 rrr r1
6 kkk k3
Name: sym2, dtype: object
print (df2.ix[~df2.sym2.isin(df1.sym1), 'sym2'].tolist())
['xxx p0', 'rrr r1', 'kkk k3']
答案 1 :(得分:1)
这是另一个更快的解决方案:
In [54]: df2.set_index('sym2').index.difference(df1.set_index('sym1').index).values
Out[54]: array(['kkk k3', 'rrr r1', 'xxx p0'], dtype=object)
或作为vanilla Python列表:
In [74]: df2.set_index('sym2').index.difference(df1.set_index('sym1').index).values.tolist()
Out[74]: ['kkk k3', 'rrr r1', 'xxx p0']
700K和500K DF的计时:
In [55]: df1 = pd.concat([df1] * 10**5, ignore_index=True)
In [57]: df2 = pd.concat([df2] * 10**5, ignore_index=True)
In [58]: df1.shape
Out[58]: (500000, 1)
In [59]: df2.shape
Out[59]: (700000, 1)
In [67]: %timeit df2.set_index('sym2').index.difference(df1.set_index('sym1').index).values
10 loops, best of 3: 123 ms per loop
In [68]: %timeit df2.ix[~df2.sym2.isin(df1.sym1), 'sym2']
1 loop, best of 3: 216 ms per loop
In [72]: %timeit df2.set_index('sym2').index.difference(df1.set_index('sym1').index).values.tolist()
10 loops, best of 3: 123 ms per loop