我想将MultiIndex
的值与整个索引进行比较,以获得用于其他处理的布尔数组。对于常规的整数索引,这可以正常工作,但是由于某种原因,它与MultiIndex
的比较结果不相等。
例如:
>>> i = pd.Index([1, 2, 3, 4, 5])
>>> i == i[0]
array([ True, False, False, False, False])
>>> i = pd.Index([(1, 2), (3, 2), (3, 3), (4, 1), (5, 2)])
>>> i == i[0]
array([False, False, False, False, False])
如何实现此比较?我确定通过循环遍历索引是可行的,但必须有更好的方法。
答案 0 :(得分:1)
这个真的引起了我的兴趣,我想我可能已经提出了一个令人满意的解决方案。它涉及到pandas.MultiIndex.difference
的使用non_matching_tuples = i.difference(pd.Index([i[0]])) # this gets all non-matching indices
desired_output = ~i.isin(non_matching_tuples) # this should give your desired output
让我知道这是否满足您的好奇心,我认为这几乎就是您想要的。