Pandas:将Series的数据类型更改为String

时间:2022-05-16 15:50:27

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

我在Python 2.7中使用Pandas的ver 0.12.0',并拥有如下数据帧:

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'])

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.

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

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

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

1)如何将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?

2)我最终将使用id来索引数据帧。与具有整数索引相比,数据帧中的String索引会减慢吗?

2 个解决方案

#1


44  

You can convert all elements of id to str using apply

您可以使用apply将id的所有元素转换为str

df.id.apply(str)

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

Edit by OP:

由OP编辑:

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

我认为这个问题与Python版本(2.7。)有关,这有效:

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

#2


13  

You must assign it, like this:-

您必须分配它,如下所示: -

df['id']= df['id'].astype(str)

#1


44  

You can convert all elements of id to str using apply

您可以使用apply将id的所有元素转换为str

df.id.apply(str)

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

Edit by OP:

由OP编辑:

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

我认为这个问题与Python版本(2.7。)有关,这有效:

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

#2


13  

You must assign it, like this:-

您必须分配它,如下所示: -

df['id']= df['id'].astype(str)