熊猫:将系列的数据类型更改为字符串
发布于 2021-01-29 19:35:09
我将Pandas’ver 0.12.0’与Python 2.7结合使用,并具有如下数据框:
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
系列由一些整数和字符串组成。它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) 如何将的所有元素转换id
为String?
2) 我最终将id
用于为数据帧建立索引。与具有整数索引相比,在数据帧中具有String索引会降低速度吗?
关注者
0
被浏览
77
1 个回答