Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wpjerrykwok/0fc9b6719cb84cbb9d8a182165bcab81 to your computer and use it in GitHub Desktop.
Save wpjerrykwok/0fc9b6719cb84cbb9d8a182165bcab81 to your computer and use it in GitHub Desktop.
StrataScratch 10368 Population Density
# Import your libraries
import pandas as pd
# Start writing code
cities_population.head()
# remove row with non-positive area
cities_population = cities_population[cities_population['area'] > 0]
# calculate population density
cities_population['density'] = cities_population['population'] / cities_population['area']
# round density to integer
cities_population['density'] = cities_population['density'].astype(int)
# identify the city with the minimum density
min_density = cities_population['density'].min()
# identify the city with the maximum density
max_density = cities_population['density'].max()
# add a column to the dataframe that indicates if the city is min_density or max_density
cities_population['min_density'] = cities_population['density'] == min_density
cities_population['max_density'] = cities_population['density'] == max_density
# display the results
cities_population
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment