0

I am trying to map out the latitude and longitude from a column of addresses from a data frame. But it keeps giving me key error 0.

for i in range(len(df['addresses'])):
    g = geocoder.arcgis(df['addresses'][i])
    coordinates.append(tuple(g.latlng))

Here is the error message

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-43-8a4466f30728> in <module>
      1 coordinates = []
      2 for i in range(len(df['addresses'])):
----> 3     g = geocoder.arcgis(df['addresses'][i])
      4     coordinates.append(tuple(g.latlng))

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __getitem__(self, key)
   1069         key = com.apply_if_callable(key, self)
   1070         try:
-> 1071             result = self.index.get_value(self, key)
   1072 
   1073             if not is_scalar(result):

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4728         k = self._convert_scalar_indexer(k, kind="getitem")
   4729         try:
-> 4730             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4731         except KeyError as e1:
   4732             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0
2
  • 1
    Can you copy in the entire error message? Commented Mar 23, 2021 at 21:09
  • I edited it. thanks Commented Mar 23, 2021 at 21:15

1 Answer 1

1

This answer has been edited after getting more information from the OP.

I'm assuming that df is a Pandas dataframe.

The problem here is that for i in range(len(df['addresses'])): gives you an integer on every loop. You're then trying to get a list from the Pandas dataframe with that integer, which leads to a KeyError.

Take out the range and len from the for loop, and you'll get an address from the addresses column. Then pass that address to geocoder.

So, as follows:

for address in df['addresses']:
    g = geocoder.arcgis(address)

Documentation of Pandas dataframes

Sign up to request clarification or add additional context in comments.

6 Comments

hmmm, interesting. but the column is not a dictionary. could you explain it more?
it returns error TypeError: 'numpy.ndarray' object is not callable
Oh right, I totally missed that we're looking at pandas dataframes here. Hmm, what about taking just for i in df['addresses'] ?
no still doesn't work. it just return 'KeyError: '3315 spenard road, anchorage'
Did you remove the [i] from row 3? for address in df['addresses']: g = geocoder.arcgis(address)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.