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.
Yet when I run it, I get an error: 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)
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'
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
:
subset[subset.bl.astype(str).str.contains("Stoke City")]
You can check type
of first value by:
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")]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.