Search code examples
pythonpandasgeopy

python dictionary key error excel


I'm sure I'm doing something rather silly, but I'm just not seeing it!

Here's the code I'm trying to run:

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

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

cityzip.xlsx contains these rows (and many more):

Postal   Latitude    Longitude
601      18.1786    -66.7518

I receive "KeyError: '601'" every time.

Eventually, I'd like to use geopy to calculate and write the zipcodes distances from a set of city coordinates into the xlsx file, so any tips or resources for the next steps are appreciated too!


Solution

  • enter image description here

    enter image description here

    Just try printing geo_dict['Longitude'][601]

    Access 601 as integer, not as string

    I don't think [0] is required.