简体   繁体   English

Pandas 错误 TypeError:无法理解数据类型

[英]Pandas error TypeError: data type not understood

I've been trying to slice a pandas dataframe using boolean indexing code like:我一直在尝试使用布尔索引代码对熊猫数据框进行切片,例如:

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")]

You can try cast to str by astype , because object can be something else as string :您可以尝试通过astypestr ,因为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")]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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