[英]'Invalid type comparison' in the code
我有一个pandas dataframe
,其中有很多列。 这些列可能具有3个值-True,False和NaN。 我正在用missing
的字符串替换NaN
。 我的其中一列的样本值如下:
ConceptTemp.ix[:,1].values
导致:
array([ True, False, False, False, True, True, True, True, False, True], dtype=bool)
请注意,此特定列没有NaN
,因此没有missing
字符串。
现在,我执行以下代码:
ConceptTemp.ix[:,1][ConceptTemp.ix[:,1] != 'missing'].values
要获得以下异常:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-47-0a0b76cf3ab5> in <module>()
----> 1 ConceptTemp.ix[:,1][ConceptTemp.ix[:,1] != 'missing'].values
E:\Anaconda2\lib\site-packages\pandas\core\ops.pyc in wrapper(self, other, axis)
724 other = np.asarray(other)
725
--> 726 res = na_op(values, other)
727 if isscalar(res):
728 raise TypeError('Could not compare %s type with Series'
E:\Anaconda2\lib\site-packages\pandas\core\ops.pyc in na_op(x, y)
680 result = getattr(x, name)(y)
681 if result is NotImplemented:
--> 682 raise TypeError("invalid type comparison")
683 except AttributeError:
684 result = op(x, y)
TypeError: invalid type comparison
有人知道如何解决吗?
任何指针将不胜感激。
正如人们评论的那样,在数组中组合类型(即带有布尔值的字符串)有点奇怪。 您将获得布尔数组可能与您想像的结果不符的结果。 但是,如果您绝对必须这样做,则可以通过以下两种方法进行。 第一个是与isin
:
In [40]: ConceptTemp.ix[:,0][~ConceptTemp.ix[:,0].isin(['missing'])].values
Out[40]:
array([ True, False, False, False, True, True, True, True, False, True], dtype=bool)
第二个是与apply
和lambda
In [41]: ConceptTemp.ix[:,0][ConceptTemp.ix[:,0].apply(lambda x: x != 'missing')].values
Out[41]:
array([ True, False, False, False, True, True, True, True, False, True], dtype=bool)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.