【问题标题】:I'm getting Key Error message in Python for Foursquare API我在 Python 中收到 Foursquare API 的关键错误消息
【发布时间】:2021-04-08 23:15:38
【问题描述】:

我正在尝试使用 Foursquare API 来获取有关场地的信息。我正在使用下面的代码收到 keyerror 消息。

def getNearbyVenues(names, latitudes, longitudes, radius=1000, LIMIT=100):
    
    venues_list=[]
    for name, lat, lng in zip(names, latitudes, longitudes):
        print(name)
            
        # create the API request URL
        url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
            CLIENT_ID, 
            CLIENT_SECRET, 
            VERSION, 
            lat, 
            lng, 
            radius, 
            LIMIT)
            
        # make the GET request
        
        results = requests.get(url).json()['response'].get('groups',[{}])[0].get('items', [])
        
        # return only relevant information for each nearby venue
        venues_list.append([(
            name, 
            lat, 
            lng, 
            v['venue']['name'], 
            v['venue']['location']['lat'], 
            v['venue']['location']['lng'],  
            v['venue']['categories'][0]['name']) for v in results])

    nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])
    nearby_venues.columns = ['Neighborhood', 
                  'Neighborhood Latitude', 
                  'Neighborhood Longitude', 
                  'Venue', 
                  'Venue Latitude', 
                  'Venue Longitude', 
                  'Venue Category']
    
    return(nearby_venues)

toronto_venues=getNearbyVenues(names=df['Neighborhood'],latitudes=df['Latitude'],longitudes=df['Longitude'])

KeyError: '邻居'

我已经等了几天,以确保我没有超出我的限制并更改了版本并重置了客户端密码。我也尝试创建一个新帐户,但我仍然收到此错误消息。

这是数据框

     Borough                Neighborhood            Latitude    Longitude
0   North York                           Parkwoods  43.753259   -79.329656
1   North York                    Victoria Village  43.725882   -79.315572
2   Downtown Toronto     Regent Park, Harbourfront  43.654260   -79.360636
3   North York     Lawrence Manor, Lawrence Heights 43.718518   -79.464763
4   Queen's Park      Ontario Provincial Government 43.662301   -79.389494
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

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

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

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Neighborhood'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-109-7784a05c5fcd> in <module>
----> 1 toronto_venues = getNearbyVenues(names=df['Neighborhood'],latitudes=df['Latitude'],longitudes=df['Longitude'])

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

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

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

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Neighborhood'

【问题讨论】:

  • 你的df是什么样的?
  • 另外请发布完整的错误回溯,而不仅仅是错误短语。

标签: python pandas jupyter-notebook foursquare keyerror


【解决方案1】:

我能够通过将代码重写到新笔记本中来解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 2016-06-03
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多