KeyError:“json文件不再支持将listlikes传递到.loc或[]以及任何缺少的标签。”

2024-10-06 15:25:41 发布

您现在位置:Python中文网/ 问答频道 /正文

{'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'},

这是我文件中关于“结果”的一部分

我那么

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']

场馆=结果['response']['groups'][0]['items']

附近的场馆=pd.json标准化(场馆)

过滤的_cols=['vention.name'、'vention.catergorities'、'vention.location.lat'、'vention.location.lng']

附近场馆=附近场馆。loc[:,过滤的场馆]

邻近场馆['vention.categories']=邻近场馆。应用(getCAT,axis=1)

附近场馆。列=[col.split(“.”)[-1]表示附近场馆中的col.columns]

附近的场馆。负责人()

I get-KeyError:“json文件不再支持传递列表likes to.loc或[]以及任何缺少的标签

如果我把那部分注释掉,它运行得很好,但效果有限。我做错了什么


Tags: namefoodresponseitemslocationlistrowgroups
1条回答
网友
1楼 · 发布于 2024-10-06 15:25:41

expndtw.DataFrame.loc

属性DataFrame.loc

Access a group of rows and columns by label(s) or a boolean array.

尝试从iltered_cols=['venue.name', 'venue.catergories', 'venue.location.lat', 'venue.location.lng']行中删除venue.

相关问题 更多 >