簡體   English   中英

如何修復 Keyerror:組?

[英]How can I fix Keyerror: Groups?

我一直在努力處理這段代碼,我需要在其中獲取有關紐約市印度餐館的數據,但是我收到錯誤消息 Keyerror: Groups。 我希望有人可以幫我解決這個問題。 我將代碼與消息一起發布,您可以在其中看到錯誤。

def get_venues(lat,lng):
    
    #set variables
    radius=1000
    LIMIT=100
    CLIENT_ID = ['FRI4BGBGLDEYD5LXOQC4YHL2JSBGEVH3YZOOWYAYQAER3JT1'] # your Foursquare ID
    CLIENT_SECRET = ['Z30R3SJZJ1STXSM0HYE2QCH3KXK2XNJI210NQXOYBHWHOHMO'] # your Foursquare Secret
    VERSION = '20200401' # Foursquare API version
    
    #url to fetch data from foursquare api
    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)
    
    # get all the data
    results = requests.get(url).json()
    venue_data = results["response"]['groups'][0]['items']
    venue_details = []
    for row in venue_data:
        try:
            venue_id=row['venue']['id']
            venue_name=row['venue']['name']
            venue_category=row['venue']['categories'][0]['name']
            venue_details.append([venue_id,venue_name,venue_category])
        except KeyError:
            pass
        
    column_names=['ID','Name','Category']
    df = pd.DataFrame(venue_details,columns=column_names)
    return df
# prepare neighborhood list that contains indian resturants
column_names=['Borough', 'Neighborhood', 'ID','Name']
indian_rest_ny=pd.DataFrame(columns=column_names)
count=1
for row in new_york_data.values.tolist():
    Borough, Neighborhood, Latitude, Longitude=row
    venues = get_venues(Latitude,Longitude)
    indian_resturants=venues[venues['Category']=='Indian Restaurant']   
    print('(',count,'/',len(new_york_data),')','Indian Resturants in '+Neighborhood+', '+Borough+':'+str(len(indian_resturants)))
    for resturant_detail in indian_resturants.values.tolist():
        id, name , category=resturant_detail
        indian_rest_ny = indian_rest_ny.append({'Borough': Borough,
                                                'Neighborhood': Neighborhood, 
                                                'ID': id,
                                                'Name' : name
                                               }, ignore_index=True)
    count+=1
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-10-7a917332b722> in <module>
      5 for row in new_york_data.values.tolist():
      6     Borough, Neighborhood, Latitude, Longitude=row
----> 7     venues = get_venues(Latitude,Longitude)
      8     indian_resturants=venues[venues['Category']=='Indian Restaurant']
      9     print('(',count,'/',len(new_york_data),')','Indian Resturants in '+Neighborhood+', '+Borough+':'+str(len(indian_resturants)))

<ipython-input-3-6da7f127fa47> in get_venues(lat, lng)
     21     results = requests.get(url).json()
     22     #results = requests.get(url).json()["response"]['groups'][0['items']
---> 23     venue_data = results["response"]['groups'][0]['items']
     24     venue_details = []
     25     for row in venue_data:

KeyError: 'groups'

僅當鍵不在字典中時才會引發KeyError 引發此錯誤的唯一方法是沒有關鍵的Groups 確保有Groups ,解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM