KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported on json file

2020-03-26 python json pandas indexing keyerror
{'meta': {'code': 200, 'requestId': '5e7c703bb9a389001b7d1e8c'},
 'response': {'suggestedFilters': {'header': 'Tap to show:',
   'filters': [{'name': 'Open now', 'key': 'openNow'}]},
  'headerLocation': 'Lagos',
  'headerFullLocation': 'Lagos',
  'headerLocationGranularity': 'city',
  'totalResults': 39,
  'suggestedBounds': {'ne': {'lat': 6.655478745000045,
    'lng': 3.355524537252914},
   'sw': {'lat': 6.565478654999954, 'lng': 3.2650912627470863}},
  'groups': [{'type': 'Recommended Places',
    'name': 'recommended',
    'items': [{'reasons': {'count': 0,
       'items': [{'summary': 'This spot is popular',
         'type': 'general',
         'reasonName': 'globalInteractionReason'}]},
      'venue': {'id': '502806dce4b0f23b021f3b77',
       'name': 'KFC',
       'location': {'lat': 6.604589745106469,
        'lng': 3.3089358809010045,
        'labeledLatLngs': [{'label': 'display',
          'lat': 6.604589745106469,
          'lng': 3.3089358809010045}],
        'distance': 672,
        'cc': 'NG',
        'city': 'Egbeda',
        'state': 'Lagos',
        'country': 'Nigeria',
        'formattedAddress': ['Egbeda', 'Lagos', 'Nigeria']},
       'categories': [{'id': '4bf58dd8d48988d16e941735',
         'name': 'Fast Food Restaurant',
         'pluralName': 'Fast Food Restaurants',
         'shortName': 'Fast Food',
         'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/fastfood_',
          'suffix': '.png'},
         'primary': True}],
       'photos': {'count': 0, 'groups': []}},
      'referralId': 'e-0-502806dce4b0f23b021f3b77-0'},

That is a part of my file about called 'results'

I then

def getCAT(row):
    try:
        categories_list=row['categories']
    except:
        categories_list=row['venue.categories']

    if len(categories_list)==0:
        return None
    else:
        return categories_list[0]['name']

venues=results['response']['groups'][0]['items']

nearby_venues=pd.json_normalize(venues)

filtered_cols=['venue.name', 'venue.catergories', 'venue.location.lat', 'venue.location.lng']

nearby_venues= nearby_venues.loc[: , filtered_cols]

nearby_venues['venue.categories']=nearby_venues.apply(getCAT, axis=1)

nearby_venues.columns=[col.split(".")[-1] for col in nearby_venues.columns]

nearby_venues.head()

I get KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported on json file.

if I comment out that part, it runs well but with limited result. What am I doing wrong?

Answers

Related