简体   繁体   中英

How to solve TypeError: 'numpy.ndarray' object is not callable on Python

I am working to aggregate Json file in python I use a list comprehension to get all the agency responsibles

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()] 

I got this error :

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.ndarray' object is not callable

Then I tried to solve this by adding numpy and dealing with array.

  import numpy as np
    np.[x.get("agency_responsible") for x in d.values()]

But it seems that it doensn't work out !

values is a property of a DataFrame, not a method. Just use d.values to access the array.

In fact, I think what you want is simply:

ar = d['agency_responsible'].values

or

ar = d.agency_responsible.values

Here's an actual session:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM