23
我想將float64的數據框索引(行)從字符串或unicode更改爲字符串。pandas - 將df.index從float64更改爲unicode或字符串
我認爲這會工作,但顯然不是:
#check type
type(df.index)
'pandas.core.index.Float64Index'
#change type to unicode
if not isinstance(df.index, unicode):
df.index = df.index.astype(unicode)
錯誤消息:
TypeError: Setting <class 'pandas.core.index.Float64Index'> dtype to anything other than float64 or object is not supported
這對Python 3.5無效。你有什麼想法,爲什麼? –
原來的海報使用的是Python 2.在Python 3中'unicode'類型不再存在,而必須使用'str'類型(基本上,Python 2中稱爲「str」的字段在Python 3和'unicode'同樣變成了'str')。 請參閱[此問題](http://stackoverflow.com/questions/19877306/nameerror-global-name-unicode-is-not-defined-in-python-3)以獲取更多信息。 – Arthur