Question

In: Computer Science

Using Python: cities = pd.DataFrame( [[2463431, 2878.52], [1392609, 5110.21], [5928040, 5905.71]], columns=['population', 'area'], index=pd.Index(['Vancouver','Calgary','Toronto'], name='city') )...

Using Python: 

cities = pd.DataFrame(
    [[2463431, 2878.52], [1392609, 5110.21], [5928040, 5905.71]],
 
    columns=['population', 'area'],
    index=pd.Index(['Vancouver','Calgary','Toronto'], name='city')
)

Do the following:

  • Estimate the density for each city
  • rank the cities from 1 to 3 with 1 is the city with the highest density and 3 is the lowest
  • Display an updated data frame with the ranking and density information upended as new columns

Solutions

Expert Solution

# The code for the required problem is given below with screenshots of code

# for indentation and output and if you feel any problem then feel free to ask

# importing pandas module with name pd

import pandas as pd

# data frame named cities

cities = pd.DataFrame(

[[2463431, 2878.52], [1392609, 5110.21], [5928040, 5905.71]],

columns=['population', 'area'],

index=pd.Index(['Vancouver','Calgary','Toronto'], name='city')

)

# empty list named density to store the calculted densities

density=[]

# calculating the densities and storing them into the list

for i in range(3):

density.append( cities.population[i]/cities.area[i])

# Appending the density column into the data frame

cities['density']=density

# appending the rank column into the data frame according

# the rank function with ascending parameter is used

# for achieving the ranking as required

cities['rank'] = cities['density'].rank(ascending=False)

# printing the updated data frame

print(cities)

OUTPUT:-


Related Solutions

James travels from Toronto to Calgary then Vancouver and finally to Prince George. There is a...
James travels from Toronto to Calgary then Vancouver and finally to Prince George. There is a 5% chance that his luggage is left behind in Toronto. If it is not left behind in Toronto, there is a 7% chance that it is left behind in Calgary. If it not left behind in Toronto or Calgary, there is a 10% chance that it is left behind in Vancouver. What is the probability his luggage is lost?
Company ABC is considering opening warehouses in four cities in Canada: Ottawa, Toronto, Calgary and Winnipeg....
Company ABC is considering opening warehouses in four cities in Canada: Ottawa, Toronto, Calgary and Winnipeg. Each warehouse can ship 100 units per day. The daily fixed cost of keeping each warehouse open is $400 for Ottawa, $500 for Toronto, $300 for Calgary and $350 for Winnipeg. Region 1 of the country requires 80 units per day, region 2 requires 70 units per day and region 3 requires 40 per day. The costs (including production and shipping costs) of sending...
You have monthly data on gasoline prices in two cities—Vancouver and Toronto, for the years 2006–2010....
You have monthly data on gasoline prices in two cities—Vancouver and Toronto, for the years 2006–2010. In each month of each year, you observe the average price of gasoline in each city. Prices in Vancouver are usually higher than in Toronto, but the cities follow similar price trends, as prices rise in the summer months and respond similarly to demand and cost shocks. However, there are month-to-month fluctuations for various reasons. Starting from January 1, 2008, Vancouver imposed a carbon...
{PYTHON }You have a CSV file containing the location and population of various cities around the...
{PYTHON }You have a CSV file containing the location and population of various cities around the world. For this question you'll be given a list of cities and return the total population across all those cities. Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a...
{PYTHON }You have a CSV file containing the location and population of various cities around the...
{PYTHON }You have a CSV file containing the location and population of various cities around the world. For this question you'll be given a list of cities and return the total population across all those cities. Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a...
The City Council of a regional area with a population of 15,240 people is committed to...
The City Council of a regional area with a population of 15,240 people is committed to do a lockdown of the region if more than 3% of its population contracts the Covid-19 virus. To investigate whether more than 3% of the population have contracted the virus, the health authority conducted a random Covid-19 test over 1000 people living in the region; 32 people tested positive to the virus. a) You were recently hired as a junior statistician working for the...
The City Council of a regional area with a population of 15,240 people is committed to...
The City Council of a regional area with a population of 15,240 people is committed to do a lockdown of the region if more than 3% of its population contracts the Covid-19 virus. To investigate whether more than 3% of the population have contracted the virus, the health authority conducted a random Covid-19 test over 1000 people living in the region; 32 people tested positive to the virus. a) You were recently hired as a junior statistician working for the...
The City Council of a regional area with a population of 15,240 people is committed to...
The City Council of a regional area with a population of 15,240 people is committed to do a lockdown of the region if more than 3% of its population contracts the Covid-19 virus. To investigate whether more than 3% of the population have contracted the virus, the health 5|Page authority conducted a random Covid-19 test over 1000 people living in the region; 32 people tested positive to the virus. a) You were recently hired as a junior statistician working for...
In this assignment, you will write a Python script to increase the population of each city...
In this assignment, you will write a Python script to increase the population of each city in the world_x database by 10% (rounded). First, add a new column to the "world_x" city table using the following MySQL command: ALTER TABLE `world_x`.`city` ADD COLUMN `Population` DOUBLE NULL DEFAULT NULL AFTER `Info`; The existing population data are stored as JSON datatype in the city table in a field named Info. You learned about JSON data types in Module 2. To obtain the...
In Python Modify the program so it contains four columns: name, year, price and rating (G,PG,R…)...
In Python Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1 print() def add(movie_list): name...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT