2016-07-24 114 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我的荣幸!!如果它为你工作,你能否接受这个答案? –