Skip to content

Instantly share code, notes, and snippets.

@lancejohnson
Last active July 3, 2020 17:58
Show Gist options
  • Select an option

  • Save lancejohnson/daf4ea0c64aa089c9996f5640ff9bc4f to your computer and use it in GitHub Desktop.

Select an option

Save lancejohnson/daf4ea0c64aa089c9996f5640ff9bc4f to your computer and use it in GitHub Desktop.
Download a range of surnames by rank from Census.gov. It calls the Census.gov Surname API (you must have an API key from the government). Here is a link to the API https://www.census.gov/data/developers/data-sets/surnames.2010.html. I chose to call the top 100,000 surnames due to frequency. Page 4 of this explanatory PDF has an excellent frequen…
import csv
import os
import requests
CENSUS_API_KEY = os.environ.get("CENSUS_API_KEY", "")
CENSUS_SURNAME_URL = "https://api.census.gov/data/2010/surname"
params = {
"key": CENSUS_API_KEY,
"get": "NAME,COUNT",
"RANK": "1:100000"
}
resp = requests.get(CENSUS_SURNAME_URL, params)
last_names = resp.json()
with open("lastnames.csv", "w") as outfile:
writer = csv.writer(outfile)
writer.writerows(last_names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment