pandas - 将df.index从float64更改为unicode或string [英] pandas - change df.index from float64 to unicode or string

查看:2654
本文介绍了pandas - 将df.index从float64更改为unicode或string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据帧的索引(行)从float64更改为string或unicode。

I want to change a dataframes' index (rows) from float64 to string or unicode.

我认为这样可行但显然不行:

I thought this would work but apparently not:

#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


推荐答案

你可以这样做:

# for Python 2
df.index = df.index.map(unicode) 

# for Python 3 (the unicode type does not exist and is replaced by str)
df.index = df.index.map(str)

至于为什么你会从你从int转换为float的时候有所不同,这是numpy(熊猫的图书馆)的特点是基于)。

As for why you would proceed differently from when you'd convert from int to float, that's a peculiarity of numpy (the library on which pandas is based).

每个numpy数组都有一个 dtype ,它基本上是其元素的机器类型:以这种方式, numpy直接处理本机类型,而不是Python对象,这解释了它如此快速。因此,当您将dtype从int64更改为float64时,numpy将在C代码中强制转换每个元素。

Every numpy array has a dtype, which is basically the machine type of its elements : in that manner, numpy deals directly with native types, not with Python objects, which explains how it is so fast. So when you are changing the dtype from int64 to float64, numpy will cast each element in the C code.

还有一个特殊的dtype: object ,它基本上会提供指向Python对象的指针。

There's also a special dtype : object, that will basically provide a pointer toward a Python object.

如果你想要字符串,你必须使用对象 dtype。但是使用 .astype(object)不会给你你想要的答案:它会创建一个带有 object dtype的索引,但是放了Python浮动对象里面。

If you want strings, you thus have to use the object dtype. But using .astype(object) would not give you the answer you were looking for : it would instead create an index with object dtype, but put Python float objects inside.

这里,通过使用map,我们将索引转换为具有相应函数的字符串:numpy获取字符串对象并理解索引必须具有一个对象 dtype,因为这是唯一可以容纳字符串的dtype。

Here, by using map, we convert the index to strings with the appropriate function: numpy gets the string objects and understand that the index has to have an object dtype, because that's the only dtype that can accomodate strings.

这篇关于pandas - 将df.index从float64更改为unicode或string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆