Python 代码中的“无效类型比较”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37999389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Invalid type comparison' in the code
提问by Patthebug
I have a pandas dataframe
which has many columns. These columns may have 3 values - True, False and NaN. I'm replcaing the NaN
with the string missing
. The sample values for one of my columns is as follows:
我有一个pandas dataframe
有很多列的。这些列可能有 3 个值 - True、False 和 NaN。我正在NaN
用字符串替换missing
。我的其中一列的示例值如下:
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.
任何指针将不胜感激。
回答by johnchase
As people have commented, it is a bit weird to combine types in your arrays (i.e. 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
第二个是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)