【问题标题】:When I run this Python code on my notebook it return empty table. I get this error How can I overcome this error?当我在笔记本上运行此 Python 代码时,它返回空表。我收到此错误如何克服此错误?
【发布时间】:2020-03-21 16:33:17
【问题描述】:

venues = results['response']['groups'][0]['items']
SGnearby_venues = json_normalize(venues) # flatten JSON
# filter columns
filtered_columns = ['venue.name', 'venue.categories', 'venue.location.lat', 'venue.location.lng']
SGnearby_venues =SGnearby_venues.loc[:, filtered_columns]
# filter the category for each row
SGnearby_venues['venue.categories'] = SGnearby_venues.apply(get_category_type, axis=1)
# clean columns
SGnearby_venues.columns = [col.split(".")[-1] for col in SGnearby_venues.columns]

SGnearby_venues.shape

这是错误消息,它并没有解释代码有什么问题,我得到的错误类型是 KeyError

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-23-a945df6402f0> in <module>
----> 1 venues = results['response']['groups'][0]['items']
      2 SGnearby_venues = json_normalize(venues) # flatten JSON
      3 # filter columns
      4 filtered_columns = ['venue.name', 'venue.categories', 'venue.location.lat', 'venue.location.lng']
      5 SGnearby_venues =SGnearby_venues.loc[:, filtered_columns]

KeyError: 'groups'

有人可以帮我渡过这个错误吗?

【问题讨论】:

  • 请包含(sn-p)结果中的数据
  • 如果您需要能够在不确定结果是否为空(或在您需要的结构中)时运行此代码,您应该使用 try: 并捕获 keyerror
  • 你能发布至少一部分results吗?
  • 这里是部分结果。结果 = requests.get(url).json()
  • 不是这样的;这就是变量声明。尝试至少发布print(results) 的部分输出。当你在它的时候,也发布print(type(results))的输出。

标签: python dataframe xpath html-table append


【解决方案1】:

密钥错误通常意味着密钥不存在。所以你的results['response'] 没有密钥'groups'

【讨论】:

  • 谢谢你,现在我知道 KeyError 是什么意思了
【解决方案2】:

我得到的 KeyError: 'groups' 是因为 CLIENT_ID、CLIENT_SECRET、VERSION,因为它们已经过时了我在那里使用的那些但在从 Foursquare 更新它们之后一切都开始正常工作了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2020-10-03
    • 2022-06-30
    • 2022-07-15
    • 2020-11-21
    • 1970-01-01
    • 2021-02-16
    相关资源
    最近更新 更多