Python/SodaPY API 返回不可哈希的类型: di

2024-09-26 22:49:46 发布

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

我使用了一个SodaPY客户端从纽约市开放政府计划下载JSON数据。SodaPY提供了使用其客户机的代码和以下注释:

# First 2000 results, returned as JSON from API / converted to Python list of
# dictionaries by sodapy.
results = client.get("qiz3-axqb", limit=2000)
# Convert to pandas DataFrame
df = pd.DataFrame.from_records(results)

当我使用方法descripe()检查数据帧时,我收到一条关于功能“location”的错误消息:

...
pandas/_libs/hashtable_func_helper.pxi in 
pandas._libs.hashtable.value_count_object()

TypeError: unhashable type: 'dict'

我试图将此数据类型解析为一对GIS坐标。这是列的第一个元素:

{u'type': u'Point', u'coordinates': [-73.917305, 40.723854]}

我曾尝试使用“enumerate”来解包,但它似乎没有像我预期的那样解包(我希望提取每个dict元素末尾的一对坐标):

In:
for (i, v) in enumerate(df['location'][:4]):
    print (i,v)
Out:
(0, {u'type': u'Point', u'coordinates': [-73.917305, 40.723854]})
(1, {u'type': u'Point', u'coordinates': [-73.871315, 40.83228]})
(2, {u'type': u'Point', u'coordinates': [-73.93294, 40.70393]})
(3, {u'type': u'Point', u'coordinates': [-73.92817, 40.714207]})

有人能帮我理解发生了什么事,并建议一种提取这些信息的Python技术吗?泰


Tags: toinfromjsondataframepandasdftype

热门问题