-2

Im trying to get the average of "active" for each place under a specific area. So say the output would be ("Andaman and Nicobar Islands": 10, "Andhra Pradesh": 12) however i get type error string indices must be integers from the "r[v["recovered"]].append(v["deceased"])"

import requests, json
from collections import defaultdict
from statistics import mean

# request and parse the data
response_API = requests.get("https://data.covid19india.org/state_district_wise.json").json()

def get_mean_active(response_API):
    r = defaultdict(list)
    for v in response_API.values():
        r[v["area"]].append(v["active"])
    return {k: mean(v) for k, v in r}


print(
    get_mean_active(
        response_API["Andaman and Nicobar Islands"]["districtData"]["top level here"]
    )
)

TypeError: string indices must be integers
PS C:\Users\e\AppData\Local\Programs\Python\Python39\test>  c:; cd 'c:\Users\e\AppData\Local\Programs\Python\Python39\test'; & 'C:\User  File "C:\Users\e\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\e\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\e\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\e\AppData\Local\Programs\Python\Python39\test\ex2.py", line 19, in <module>
    get_mean_active(
  File "c:\Users\e\AppData\Local\Programs\Python\Python39\test\ex2.py", line 14, in get_mean_active
    r[v["Andaman and Nicobar Islands"]].append(v["active"])
TypeError: string indices must be integers
PS C:\Users\e\AppData\Local\Programs\Python\Python39\test>
11
  • If this code works, why are you here? If not, explain the specifics of the problem. Commented Apr 21, 2022 at 19:30
  • @ScottHunter it doesnt work i cant seem to pull the specific data to form the desired output, im currently getting Typerror: string indices must be integers. Commented Apr 21, 2022 at 19:35
  • I get a different error. There exists no r[v["recovered"]].append(v["deceased"]) in the provided code. Please post the actual code and a full stack trace. Commented Apr 21, 2022 at 19:40
  • @MichaelRuth Sorry about that, my mistake. now ive updated it to what it should be. Commented Apr 21, 2022 at 19:49
  • What is top level here? I don't see that in the JSON. Are you replacing that with a district name? Commented Apr 21, 2022 at 19:50

1 Answer 1

0

The argument you're passing is going too deep into the nested dictionaries. You want to pass the entire districtData dictionary to get_mean_active(), not a specific area. Then the function loops over all the areas.

The loop should use .items(), not .values(), because the area name is the key of each dictionary element, not an element of the nested dictionary.

And the dictionary comprehension at the end needs to use r.items().

import requests, json
from collections import defaultdict
from statistics import mean

# request and parse the data
response_API = requests.get("https://data.covid19india.org/state_district_wise.json").json()

def get_mean_active(response_API):
    r = defaultdict(list)
    for area, v in response_API.items():
        r[area].append(v["active"])
    return {k: mean(v) for k, v in r.items()}


print(
    get_mean_active(
        response_API["Andaman and Nicobar Islands"]["districtData"]
    )
)

Result:

{'Nicobars': 0, 'North and Middle Andaman': 0, 'South Andaman': 19, 'Unknown': -13}
Sign up to request clarification or add additional context in comments.

4 Comments

Very much appreciate your assistance @Barmar the reason i used mean was because i was going through a level up from your code. So say, the results from your output would all be in one key {"Andaman and Nicobar Islands": 6}, {"Andhra Pradesh": 1227 } getting the area and the average to all witin the area.
But you're calling the function on just Andaman and Nicobar Islands. It seems like you should be calculating the mean outside the function, when you loop over the districts.
Or maybe you just need return mean(v['active'] for v in response_API.values())
Currenlty trying those approaches, this has been a great help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.