Created
April 23, 2024 15:39
-
-
Save wpjerrykwok/0fc9b6719cb84cbb9d8a182165bcab81 to your computer and use it in GitHub Desktop.
StrataScratch 10368 Population Density
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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