简体   繁体   English

代码中的“无效类型比较”

[英]'Invalid type comparison' in the code

I have a pandas dataframe which has many columns. 我有一个pandas dataframe ,其中有很多列。 These columns may have 3 values - True, False and NaN. 这些列可能具有3个值-True,False和NaN。 I'm replcaing the NaN with the string missing . 我正在用missing的字符串替换NaN The sample values for one of my columns is as follows: 我的其中一列的样本值如下:

ConceptTemp.ix[:,1].values

resulting in: 导致:

array([ True, False, False, False,  True,  True,  True,  True, False,  True], dtype=bool)

Note that this particular column had no NaN , and therefore no missing string. 请注意,此特定列没有NaN ,因此没有missing字符串。

Now I execute the following code: 现在,我执行以下代码:

ConceptTemp.ix[:,1][ConceptTemp.ix[:,1] != 'missing'].values

To get the following exception: 要获得以下异常:

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

Would someone know how to fix it? 有人知道如何解决吗?

Any pointers would be highly appreciated. 任何指针将不胜感激。

As people have commented, it is a bit weird to combine types in your arrays (ie strings with booleans). 正如人们评论的那样,在数组中组合类型(即带有布尔值的字符串)有点奇怪。 You're going to get results where the boolean array may not be what you think it is. 您将获得布尔数组可能与您想像的结果不符的结果。 But if you absolutely have to, there are a couple of ways you could go about doing this. 但是,如果您绝对必须这样做,则可以通过以下两种方法进行。 The first is with isin : 第一个是与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)

The second is with apply and lambda 第二个是与applylambda

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM