Skip to content

Instantly share code, notes, and snippets.

@meli-lewis
Last active May 14, 2019 00:37
Show Gist options
  • Save meli-lewis/c8099bf28f3cdcf3beeec359e1a49bea to your computer and use it in GitHub Desktop.
Save meli-lewis/c8099bf28f3cdcf3beeec359e1a49bea to your computer and use it in GitHub Desktop.
Population Estimate Analysis for Finding Incorporated Places with Population < 800
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()
@meli-lewis
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment