python中数据帧到str的转换
从“zipcode”下的数据帧中,我希望使用python将下一列的最后2位数字更改为python中数据帧到str的转换,python,Python,从“zipcode”下的数据帧中,我希望使用python将下一列的最后2位数字更改为00: 850xx 931xx 933xx 113xx 440xx 186xx 327xx data['zipcode'].astype(str)将zipcode列转换为string zipc[:3]取zipc的前3个字符 像这样的 In[90]: data['zipcode'] = 85055 In[91]: data['zipcode'] = [zipc[:3] + '00' for zipc in da
00
:
850xx
931xx
933xx
113xx
440xx
186xx
327xx
data['zipcode'].astype(str)
将zipcode列转换为string
zipc[:3]
取zipc的前3个字符
像这样的
In[90]: data['zipcode'] = 85055
In[91]: data['zipcode'] = [zipc[:3] + '00' for zipc in data['zipcode'].astype(str).values]
In[92]: data['zipcode']
Out[92]:
Index
1 85000
2 85000
3 85000
4 85000
5 85000
6 85000
7 85000
8 85000
9 85000
10 85000
Name: zipcode, dtype: object
谢谢凯文的编辑。@JaydenYuen:我在帖子中添加了一些解释。如果需要,在代码的某一部分问一个精确的问题。我不知道zipc做什么。另外,当我试图将zipcode排序为不同的数据帧时。zero=[]表示data.zip_代码中的i:如果i<9999:zero.append(i)print zero它只打印[]@JaydenYuen:zipc在for语句中,该语句在data.zipcode.values定义的数组上循环。看一看