这里你应该用
astype(str)
trainer_pairs['zip5'] = trainer_pairs.zip5.astype(str)
关于你的错误
df=pd.DataFrame({'zip':[1,2,3,4,5]})
df.zip.astype(object)
Out[4]:
0 1
1 2
2 3
3 4
4 5
Name: zip, dtype: object
即使转换为对象,它们仍然
int
,使用类型执行切片
int
或
float
将返回值为
NaN
. 请检查
df.zip.astype(object).apply(type)
Out[5]:
0 <class 'int'>
1 <class 'int'>
2 <class 'int'>
3 <class 'int'>
4 <class 'int'>
Name: zip, dtype: object
df.zip.astype(str).apply(type)
Out[6]:
0 <class 'str'>
1 <class 'str'>
2 <class 'str'>
3 <class 'str'>
4 <class 'str'>
Name: zip, dtype: object