Pandas 错误 TypeError:无法理解数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38073659/
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
Pandas error TypeError: data type not understood
提问by elksie5000
I've been trying to slice a pandas dataframe using boolean indexing code like:
我一直在尝试使用布尔索引代码对 Pandas 数据框进行切片,例如:
subset[subset.bl.str.contains("Stoke City")]
The column bl
is of 'object' dtype.
该列bl
是 'object' dtype。
Yet when I run it, I get an error: TypeError: data type not understood
然而,当我运行它时,我收到一个错误: TypeError: data type not understood
How do I fix this?
我该如何解决?
UPDATE:
更新:
I tried using:
我尝试使用:
subset[subset.bl.astype(str).str.contains("Stoke City")]
But that gave: UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 37: ordinal not in range(128)
但这给了: UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 37: ordinal not in range(128)
I then tried fixing that with:
然后我尝试使用以下方法修复它:
subset.bl = subset.bl.str.encode("utf-8")
That seemed to work, but I got the same error: 'data type not understood error'
这似乎有效,但我遇到了同样的错误: 'data type not understood error'
when I again tried:
当我再次尝试时:
subset[subset.bl.astype(str).str.contains("Stoke City")]
回答by jezrael
You can try cast to str
by astype
, because object
can be something else as string
:
您可以尝试强制转换为str
by astype
,因为object
可以是其他内容string
:
subset[subset.bl.astype(str).str.contains("Stoke City")]
You can check type
of first value by:
您可以type
通过以下方式检查第一个值:
type(subset.ix[0, 'bl'])
EDIT:
编辑:
You can try:
你可以试试:
subset[subset.bl.str.encode("utf-8").str.contains("Stoke City")]
Or:
或者:
subset['bl'] = subset.bl.str.encode("utf-8")
subset[subset.bl.str.contains("Stoke City")]