我想将一个数据框的索引(行)从float64更改为string或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
unicode
类型已经不存在了,必须使用str
类型代替(基本上,在 Python 2 中称为str
的东西,在 Python 3 中被称为bytes
,而unicode
同样变成了str
)。 更多信息请参见这个问题。 - Salomé