Skip to content

Instantly share code, notes, and snippets.

@nbroad1881
Last active February 22, 2021 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbroad1881/62b55b2756eb8e3043c81316c648cb3b to your computer and use it in GitHub Desktop.
Save nbroad1881/62b55b2756eb8e3043c81316c648cb3b to your computer and use it in GitHub Desktop.
Go from a 2 letter state abbreviation to the region. Territories included https://en.wikipedia.org/wiki/List_of_U.S._state_and_territory_abbreviations. Regions defined here: https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
# full name to abbreviation here: https://gist.github.com/mshafrir/2646763
ste_to_reg = {
'AA': 'Other',
'AE': 'Other',
'AP': 'Other',
'AK': 'West',
'AL': 'South',
'AR': 'South',
'AS': 'Other',
'AZ': 'West',
'CA': 'West',
'CO': 'West',
'CT': 'Northeast',
'DC': 'South',
'DE': 'South',
'FL': 'South',
'FM': 'Other',
'GA': 'South',
'GU': 'Other',
'HI': 'West',
'IA': 'Midwest',
'ID': 'West',
'IL': 'Midwest',
'IN': 'Midwest',
'KS': 'Midwest',
'KY': 'South',
'LA': 'South',
'MA': 'Northeast',
'MD': 'South',
'ME': 'Northeast',
'MH': 'Other',
'MI': 'Midwest',
'MN': 'Midwest',
'MO': 'Midwest',
'MP': 'Other',
'MS': 'South',
'MT': 'West',
'NA': 'Other',
'NC': 'South',
'ND': 'Midwest',
'NE': 'Midwest',
'NH': 'Northeast',
'NJ': 'Northeast',
'NM': 'West',
'NV': 'West',
'NY': 'Northeast',
'OH': 'Midwest',
'OK': 'South',
'OR': 'West',
'PA': 'Northeast',
'PR': 'Other',
'PR': 'Other',
'RI': 'Northeast',
'SC': 'South',
'SD': 'Midwest',
'TN': 'South',
'TX': 'South',
'UM': 'Other',
'UT': 'West',
'VA': 'South',
'VI': 'Other',
'VT': 'Northeast',
'WA': 'West',
'WI': 'Midwest',
'WV': 'South',
'WY': 'West'
}
# pandas transform from list of 2 letter abbreviations to regions
df["region"] = df["state"].apply(lambda x: ste_to_reg[x])
# or if you want as a list
midwest = ['IA', 'IL', 'IN', 'KS', 'MI', 'MN', 'MO', 'ND', 'NE', 'OH', 'SD', 'WI']
northeast = ['CT', 'MA', 'ME', 'NH', 'NJ', 'NY', 'PA', 'RI', 'VT']
west = ['AK', 'AZ', 'CA', 'CO', 'HI', 'ID', 'MT', 'NM', 'NV', 'OR', 'UT', 'WA', 'WY']
other = ['AA', 'AE', 'AP', 'AS', 'FM', 'GU', 'MH', 'MP', 'NA', 'PR', 'UM', 'VI']
south = ['AL', 'AR', 'DC', 'DE', 'FL', 'GA', 'KY', 'LA', 'MD', 'MS', 'NC', 'OK', 'SC', 'TN', 'TX', 'VA', 'WV']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment