2016-07-24 81 views
1

我確定我做的事情相當愚蠢,但我只是沒有看到它!python字典關鍵錯誤excel

這裏是我試圖運行代碼:

import pandas as pd 
geo_dic = pd.read_excel('cityzip.xlsx', index_col=0).to_dict() 

print geo_dic[' Longitude']['601'][0] 

cityzip.xlsx包含這些行(還有更多):

Postal Latitude Longitude 
601  18.1786 -66.7518 

我收到 「KeyError異常: '601'」 每時間。

最後,我想使用geopy來計算並將一組城市座標中的zip代碼距離寫入xlsx文件,因此,下一步的任何提示或資源都將得到讚賞!

+0

如果沒有更多關於'geo_dic'的外觀信息,很難分辨出來。你可以添加什麼'geo_dic.head()'返回? – mgilbert

+0

'print geo_dic.head() AttributeError:'dict'對象沒有屬性'head'' – taylorhamcheese

+0

對不起,我誤讀了,我的意思是在轉換爲字典之前運行該數據框 – mgilbert

回答

0

enter image description here

enter image description here

剛剛嘗試打印geo_dict [ '東經'] [601]

訪問601的整數,而不是字符串

我不認爲[0]是必須的。

+0

謝謝!這工作:) – taylorhamcheese

+0

@ TylerHudson-Crimi我的榮幸!!如果它爲你工作,你能否接受這個答案? –