我使用了一个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技术吗?泰
好吧,我想出来了。我引用了“坐标”键。不确定为什么会出现另一个键/值对,因为所有的数据都是“点”。可能是索达皮团队的一个
相关问题 更多 >
编程相关推荐