【发布时间】:2016-03-31 10:29:48
【问题描述】:
我希望比较数据帧中的两个系列,并得到一个布尔值 True 或 False 来判断它们是否具有完全相同的元素。
如果一个元素不同,那么我想知道它的索引号。
谢谢!
【问题讨论】:
-
你能添加任何例子吗?
-
对不起,我是堆栈溢出的新手。我的代码发布在下面,(从顶部开始的第二个答案)。
标签: python pandas compare series
我希望比较数据帧中的两个系列,并得到一个布尔值 True 或 False 来判断它们是否具有完全相同的元素。
如果一个元素不同,那么我想知道它的索引号。
谢谢!
【问题讨论】:
标签: python pandas compare series
IIUC 你可以使用isin:
In [123]:
s1 = pd.Series(np.arange(5))
s2 = pd.Series(np.arange(1,6))
s2
Out[123]:
0 1
1 2
2 3
3 4
4 5
dtype: int32
In [125]:
s1.isin(s2)
Out[125]:
0 False
1 True
2 True
3 True
4 True
dtype: bool
从上面您可以通过使用~ 否定掩码来获得False 的索引值:
In [127]:
s1[~s1.isin(s2)].index
Out[127]:
Int64Index([0], dtype='int64')
【讨论】:
EdChum,感谢您的回答!
它比我设法解决的要好,无论如何我都会在下面发布:
ser1 = Series(np.arange(16))
arr = ser1.reshape(4,4)
df = DataFrame((arr),columns=['a','b','c','d'])
ser_e = Series([2,6,10,14])
df['e'] = ser_e
df['c']>df['b']
df.loc[df['c'] != df['e'] ]
【讨论】: