【问题标题】:How do I verify that two series of a pandas dataFrame have the same elements?如何验证两个系列的 pandas dataFrame 是否具有相同的元素?
【发布时间】:2016-03-31 10:29:48
【问题描述】:

我希望比较数据帧中的两个系列,并得到一个布尔值 True 或 False 来判断它们是否具有完全相同的元素。

如果一个元素不同,那么我想知道它的索引号。

谢谢!

【问题讨论】:

  • 你能添加任何例子吗?
  • 对不起,我是堆栈溢出的新手。我的代码发布在下面,(从顶部开始的第二个答案)。

标签: python pandas compare series


【解决方案1】:

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')

【讨论】:

    【解决方案2】:

    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'] ]
    

    【讨论】:

      猜你喜欢
      • 2018-03-02
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      相关资源
      最近更新 更多