我正在使用python聚合Json文件 我使用列表理解来让所有代理商负责
import pandas as pd
import numpy as np
url = "http://311api.cityofchicago.org/open311/v2/requests.json";
d= pd.read_json(url)
ar = [x.get("agency_responsible") for x in d.values()]
我收到了这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.ndarray' object is not callable
然后我尝试通过添加numpy和处理数组来解决这个问题。
import numpy as np
np.[x.get("agency_responsible") for x in d.values()]
但似乎它没有成功!
答案 0 :(得分:3)
values
是DataFrame的属性,而不是方法。只需使用d.values
即可访问该阵列。
事实上,我认为你想要的只是:
ar = d['agency_responsible'].values
或
ar = d.agency_responsible.values
这是一个实际的会话:
In [1]: import pandas as pd
In [2]: url = "http://311api.cityofchicago.org/open311/v2/requests.json"
In [3]: d = pd.read_json(url)
In [4]: type(d)
Out[4]: pandas.core.frame.DataFrame
In [5]: ar = d.agency_responsible.values
In [6]: ar[0]
Out[6]: u'Bureau of Street Operations - Graffiti'
In [7]: ar[:4]
Out[7]:
array([u'Bureau of Street Operations - Graffiti',
u'Division of Electrical Operations CDOT',
u'Bureau of Rodent Control - S/S',
u'Division of Electrical Operations CDOT'], dtype=object)