0

I have a list of dictionaries containing information about each of the London Boroughs. I am supposed to determine the mean and median population, area, and population density of all of the boroughs. I feel that perhaps the best way to do this would be to just assemble the population and area values into lists, but I don't know how.

Here's the data I imported, the file is called 'boroughs.'

[{'area': 13.93, 'name': 'Barking and Dagenham', 'population': 194352},
 {'area': 33.49, 'name': 'Barnet', 'population': 369088},
 {'area': 23.38, 'name': 'Bexley', 'population': 236687},
 {'area': 16.7, 'name': 'Brent', 'population': 317264},
 {'area': 57.97, 'name': 'Bromley', 'population': 317899},
 {'area': 8.4, 'name': 'Camden', 'population': 229719},
 {'area': 33.41, 'name': 'Croydon', 'population': 372752},
 {'area': 21.44, 'name': 'Ealing', 'population': 342494},
 {'area': 31.74, 'name': 'Enfield', 'population': 320524},
 {'area': 18.28, 'name': 'Greenwich', 'population': 264008},
 {'area': 7.36, 'name': 'Hackney', 'population': 257379},
 {'area': 6.33, 'name': 'Hammersmith and Fulham', 'population': 178685},
 {'area': 11.42, 'name': 'Haringey', 'population': 263386},
 {'area': 19.49, 'name': 'Harrow', 'population': 243372},
 {'area': 43.35, 'name': 'Havering', 'population': 242080},
 {'area': 44.67, 'name': 'Hillingdon', 'population': 286806},
 {'area': 21.61, 'name': 'Hounslow', 'population': 262407},
 {'area': 5.74, 'name': 'Islington', 'population': 215667},
 {'area': 4.68, 'name': 'Kensington and Chelsea', 'population': 155594},
 {'area': 14.38, 'name': 'Kingston upon Thames', 'population': 166793},
 {'area': 10.36, 'name': 'Lambeth', 'population': 314242},
 {'area': 13.57, 'name': 'Lewisham', 'population': 286180},
 {'area': 14.52, 'name': 'Merton', 'population': 203223},
 {'area': 13.98, 'name': 'Newham', 'population': 318227},
 {'area': 21.78, 'name': 'Redbridge', 'population': 288272},
 {'area': 22.17, 'name': 'Richmond upon Thames', 'population': 191365},
 {'area': 11.14, 'name': 'Southwark', 'population': 298464},
 {'area': 16.93, 'name': 'Sutton', 'population': 195914},
 {'area': 7.63, 'name': 'Tower Hamlets', 'population': 272890},
 {'area': 14.99, 'name': 'Waltham Forest', 'population': 265797},
 {'area': 13.23, 'name': 'Wandsworth', 'population': 310516},
 {'area': 8.29, 'name': 'Westminster', 'population': 226841},
 {'area': 1.12, 'name': 'City of London', 'population': 7000}]

So far I've tried to do a for loop, but it's not going well, and I keep printing out all of the population values but they can't be used to calculate anything because of the format they print in:

for item in boroughs:
     name = item['name']
     population = item['population']
     area = item['area']
     print(population)

The result looks like this.

I'm a beginner so please explain in very simple terms ):

kaeli
  • 1
  • 2
  • What format do they print in? Also can wee see your code? – Holden Apr 29 '20 at 22:32
  • My only code so far is this: `from boroughs import boroughs' 'import requests' 'print(boroughs)' all my code after that has been like nonfunctional, do you still want to see it? – kaeli Apr 29 '20 at 22:33
  • What do you mean by "the format they print in"? Are they strings or what? – Holden Apr 29 '20 at 22:36
  • Yes, please show all relevant code even if it is non-funtional. You could just read it into a pandas dataframe and call `mean()` on the columns. Other option is to use a list comprehension to extract the values. – Cleb Apr 29 '20 at 22:39

3 Answers3

1

It is easiest to use a pandas dataframe:

import pandas as pd
df = pd.DataFrame(boroughs)
df["density"] = df.population / df.area
print(df.describe())   # presents several useful statistics
print(df.area.mean())
print(df.density.median())

Alternatively use the standard library's statistics module:

from statistics import mean, median
print(mean(borough["area"] for borough in boroughs))
print(median(borough["population"] / borough["area"] for borough in boroughs)) # density

or similarly from numpy:

from numpy import mean, median
print(median([borough["population"] for borough in boroughs])) 

There are previous questions with additional functions to calculate mean and median in case you don't want to use these modules for some reason.

Stuart
  • 7,007
  • 1
  • 16
  • 25
0

This is just a basic for loop. If you have an int string,e.g."123", make sure to convert with int

total_pop = 0
for city in list:
    pop = int(city['population'])
    total_pop += pop
Peter S
  • 487
  • 1
  • 4
  • 17
0

I think the following might work. So as you said you want to form a list of populations and areas so that you can iterate over it to find the mean and median right?

areas = []
population = []

for x in boroughs:
    areas.append(x['area'])
    population.append(x['population'])

So now you have the list of area and population and you can find the median and mean from it.

EDIT : Now for the mean of the population, you can simply do the following.

mean_of_population = sum(population)/len(population)

And for Median

n = len(population)
if n%2==0 : #ie if the number of element in the list population are even.
    median  = (population[n//2] + population[n//2 - 1])/2
else:
    median = population[n//2]
sponge
  • 31
  • 4
  • I cannot comment on your post for now, So I am writing here. Yeah, so the output is printed correctly. The output that you have edited here shows the values of population for every dictionary present in the list boroughs in a new line. – sponge Apr 29 '20 at 22:43