Last active
May 14, 2019 00:37
-
-
Save meli-lewis/c8099bf28f3cdcf3beeec359e1a49bea to your computer and use it in GitHub Desktop.
Population Estimate Analysis for Finding Incorporated Places with Population < 800
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 pandas as pd | |
# Read fails if encoding is left to default | |
df = pd.read_csv( | |
'sub-est2017_all.csv', | |
encoding="ISO-8859-1", | |
dtype={ | |
"SUMLEV": str, | |
"STATE": str, | |
"COUNTY": str, | |
"PLACE": str | |
}) | |
# Subset data to incorporated places | |
df = df[df['SUMLEV'] == '162'] | |
# Concatenate fields to identify unique places | |
df['unique_place'] = df['STATE'].str.cat(df[['COUNTY', 'PLACE']]) | |
# Lazily perform calculation with unique values meeting critera / overall | |
df[df['POPESTIMATE2017'] < 800]['unique_place'].nunique() / df['unique_place'].nunique() | |
# Lazily calculate the population of people living in such places | |
df[df['POPESTIMATE2017'] < 800]['POPESTIMATE2017'].sum() / df['POPESTIMATE2017'].sum() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Data Source: City and Town Population Totals: 2010-2017