2015-11-08 65 views
0

從下「郵政編碼」數據幀,我很想用Python來改變從下面的列中的最後2位數字爲00轉換數據幀到STR在Python

850xx 
931xx 
933xx 
113xx 
440xx 
186xx 
327xx 
+0

感謝Kevin的編輯。 –

回答

0

data['zipcode'].astype(str)的郵政編碼列轉換爲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 
+0

明白了。任何人都可以解釋如何工作? –

+0

@JaydenYuen:我在帖子中添加了一些解釋。如果需要,請在代碼的一部分上提出確切的問題。 –

+0

我不知道zipc是做什麼的。 也當我試圖將郵政編碼分類到不同的數據框。 零= [] 對於i在data.zip_code: 如果I <9999: zero.append(ⅰ) 打印零 它只打印[] –