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

13,037

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)
Share:
13,037
user3001937
Author by

user3001937

Updated on November 25, 2022

Comments

  • user3001937
    user3001937 over 1 year

    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 !

  • user3001937
    user3001937 over 10 years
    How can we delete the duplicates this way
  • Warren Weckesser
    Warren Weckesser over 10 years
    Works for me--see my updated answer. How to remove duplicates sounds like a new question.
  • user3001937
    user3001937 over 10 years
    Yes! 'cause the first methodes has been created to avoid deplicates
  • Warren Weckesser
    Warren Weckesser over 10 years
    What do you mean by "the first methodes"? I don't see anything in your original question about removing duplicates.
  • user3001937
    user3001937 over 10 years
    np.[x.get("agency_responsible") for x in d.values()]
  • Warren Weckesser
    Warren Weckesser over 10 years
    That bit of code doesn't work, so I can't guess what you think it is supposed to do. Please edit your question to explain exactly what you are trying to accomplish.