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

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

问题描述

我想将数据帧的索引(行)从 float64 更改为字符串或 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(pandas 所基于的库)的一个特点.

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.

还有一个特殊的数据类型:object,它基本上会提供一个指向 Python 对象的指针.

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

如果您想要字符串,则必须使用 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 获取字符串对象并理解索引必须具有 object 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天全站免登陆