pandas :将系列的数据类型更改为字符串 [英] Pandas: change data type of Series to String

查看:127
本文介绍了 pandas :将系列的数据类型更改为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Pandas'ver 0.12.0'与Python 2.7结合使用,并具有如下数据框:

I use Pandas 'ver 0.12.0' with Python 2.7 and have a dataframe as below:

df = pd.DataFrame({'id' : [123,512,'zhub1', 12354.3, 129, 753, 295, 610],
                    'colour': ['black', 'white','white','white',
                            'black', 'black', 'white', 'white'],
                    'shape': ['round', 'triangular', 'triangular','triangular','square',
                                        'triangular','round','triangular']
                    },  columns= ['id','colour', 'shape'])

id系列由一些整数和字符串组成.默认情况下,其dtypeobject.我想将id的所有内容转换为字符串.我尝试了astype(str),它会在下面产生输出.

The id Series consists of some integers and strings. Its dtype by default is object. I want to convert all contents of id to strings. I tried astype(str), which produces the output below.

df['id'].astype(str)
0    1
1    5
2    z
3    1
4    1
5    7
6    2
7    6

1)如何将id的所有元素转换为String?

1) How can I convert all elements of id to String?

2)我最终将使用id为数据帧建立索引.与具有整数索引相比,在数据帧中具有String索引会减慢速度吗?

2) I will eventually use id for indexing for dataframes. Would having String indices in a dataframe slow things down, compared to having an integer index?

推荐答案

您可以使用apply

df.id.apply(str)

0        123
1        512
2      zhub1
3    12354.3
4        129
5        753
6        295
7        610

通过OP进行

我认为该问题与Python版本(2.7.)有关,这可行:

I think the issue was related to the Python version (2.7.), this worked:

df['id'].astype(basestring)
0        123
1        512
2      zhub1
3    12354.3
4        129
5        753
6        295
7        610
Name: id, dtype: object

这篇关于 pandas :将系列的数据类型更改为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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