Skip to content

Instantly share code, notes, and snippets.

@rogerallen
Last active April 19, 2024 14:04
Show Gist options
  • Save rogerallen/1583593 to your computer and use it in GitHub Desktop.
Save rogerallen/1583593 to your computer and use it in GitHub Desktop.
A Python Dictionary to translate US States to Two letter codes
# United States of America Python Dictionary to translate States,
# Districts & Territories to Two-Letter codes and vice versa.
#
# Canonical URL: https://gist.github.com/rogerallen/1583593
#
# Dedicated to the public domain. To the extent possible under law,
# Roger Allen has waived all copyright and related or neighboring
# rights to this code. Data originally from Wikipedia at the url:
# https://en.wikipedia.org/wiki/ISO_3166-2:US
#
# Automatically Generated 2021-09-11 18:04:36 via Jupyter Notebook from
# https://gist.github.com/rogerallen/d75440e8e5ea4762374dfd5c1ddf84e0
us_state_to_abbrev = {
"Alabama": "AL",
"Alaska": "AK",
"Arizona": "AZ",
"Arkansas": "AR",
"California": "CA",
"Colorado": "CO",
"Connecticut": "CT",
"Delaware": "DE",
"Florida": "FL",
"Georgia": "GA",
"Hawaii": "HI",
"Idaho": "ID",
"Illinois": "IL",
"Indiana": "IN",
"Iowa": "IA",
"Kansas": "KS",
"Kentucky": "KY",
"Louisiana": "LA",
"Maine": "ME",
"Maryland": "MD",
"Massachusetts": "MA",
"Michigan": "MI",
"Minnesota": "MN",
"Mississippi": "MS",
"Missouri": "MO",
"Montana": "MT",
"Nebraska": "NE",
"Nevada": "NV",
"New Hampshire": "NH",
"New Jersey": "NJ",
"New Mexico": "NM",
"New York": "NY",
"North Carolina": "NC",
"North Dakota": "ND",
"Ohio": "OH",
"Oklahoma": "OK",
"Oregon": "OR",
"Pennsylvania": "PA",
"Rhode Island": "RI",
"South Carolina": "SC",
"South Dakota": "SD",
"Tennessee": "TN",
"Texas": "TX",
"Utah": "UT",
"Vermont": "VT",
"Virginia": "VA",
"Washington": "WA",
"West Virginia": "WV",
"Wisconsin": "WI",
"Wyoming": "WY",
"District of Columbia": "DC",
"American Samoa": "AS",
"Guam": "GU",
"Northern Mariana Islands": "MP",
"Puerto Rico": "PR",
"United States Minor Outlying Islands": "UM",
"U.S. Virgin Islands": "VI",
}
# invert the dictionary
abbrev_to_us_state = dict(map(reversed, us_state_to_abbrev.items()))
@squeakyboots
Copy link

Those east of the Mississippi River:
['AL', 'CT', 'DE', 'FL', 'GA', 'IL', 'IN', 'KY', 'ME', 'MD', 'MA', 'MI', 'MS', 'NH', 'NJ', 'NY', 'NC', 'OH', 'PA', 'RI', 'SC', 'TN', 'VT', 'VA', 'WV', 'WI']

@khurchla
Copy link

Just what I need, cheers for creating this and sharing it!

@semvijverberg
Copy link

Hereby an auto enumerator.

from enum import IntEnum, auto

class US_States(IntEnum):
    AL = auto() # Alabama
    AK = auto() # Alaska
    AS = auto() # American Samoa
    AZ = auto() # Arizona
    AR = auto() # Arkansas
    CA = auto() # California
    CO = auto() # Colorado
    CT = auto() # Connecticut
    DE = auto() # Delaware
    DC = auto() # District of Columbia
    FL = auto() # Florida
    GA = auto() # Georgia
    GU = auto() # Guam
    HI = auto() # Hawaii
    ID = auto() # Idaho
    IL = auto() # Illinois
    IN = auto() # Indiana
    IA = auto() # Iowa
    KS = auto() # Kansas
    KY = auto() # Kentucky
    LA = auto() # Louisiana
    ME = auto() # Maine
    MD = auto() # Maryland
    MA = auto() # Massachusetts
    MI = auto() # Michigan
    MN = auto() # Minnesota
    MS = auto() # Mississippi
    MO = auto() # Missouri
    MT = auto() # Montana
    NE = auto() # Nebraska
    NV = auto() # Nevada
    NH = auto() # New Hampshire
    NJ = auto() # New Jersey
    NM = auto() # New Mexico
    NY = auto() # New York
    NC = auto() # North Carolina
    ND = auto() # North Dakota
    MP = auto() # Northern Mariana Islands
    OH = auto() # Ohio
    OK = auto() # Oklahoma
    OR = auto() # Oregon
    PA = auto() # Pennsylvania
    PR = auto() # Puerto Rico
    RI = auto() # Rhode Island
    SC = auto() # South Carolina
    SD = auto() # South Dakota
    TN = auto() # Tennessee
    TX = auto() # Texas
    UT = auto() # Utah
    VT = auto() # Vermont
    VI = auto() # Virgin Islands
    VA = auto() # Virginia
    WA = auto() # Washington
    WV = auto() # West Virginia
    WI = auto() # Wisconsin
    WY = auto() # Wyoming

@leotraeg
Copy link

Here is one to encode US states based on their population ranking (might be usefull for some further correlation analysis):
us_state_to_population_ranking_encoding = {
"CA": 52, "TX": 51, "FL": 50, "NY": 49, "PA": 48, "IL": 47,
"OH": 46, "GA": 45, "NC": 44, "MI": 43, "NJ": 42, "VA": 41,
"WA": 40, "AZ": 39, "TN": 38, "MA": 37, "IN": 36, "MO": 35,
"MD": 34, "CO": 33, "WI": 32, "MN": 31, "SC": 30, "AL": 29,
"LA": 28, "KY": 27, "OR": 26, "OK": 25, "CT": 24, "UT": 23,
"PR": 22, "NV": 21, "IA": 20, "AR": 19, "MS": 18, "KS": 17,
"NM": 16, "NE": 15, "ID": 14, "WV": 13, "HI": 12, "NH": 11,
"ME": 10, "MT": 9, "RI": 8, "DE": 7, "SD": 6, "ND": 5,
"AK": 4, "DC": 3, "VT": 2, "WY": 1
}

@elishevab
Copy link

Thank you!

@AnnieW2014
Copy link

Thank you!! And Happy New Year!

@Tetfretguru
Copy link

I just came in today to parse legal data from all US states and this came very in handy! Thanks buddy!

@shikhagoenkahf
Copy link

us_state_to_abbrev = {
"Alabama": "AL",
"Alaska": "AK",
"Arizona": "AZ",
"Arkansas": "AR",
"California": "CA",
"Colorado": "CO",
"Connecticut": "CT",
"Delaware": "DE",
"Florida": "FL",
"Georgia": "GA",
"Hawaii": "HI",
"Idaho": "ID",
"Illinois": "IL",
"Indiana": "IN",
"Iowa": "IA",
"Kansas": "KS",
"Kentucky": "KY",
"Louisiana": "LA",
"Maine": "ME",
"Maryland": "MD",
"Massachusetts": "MA",
"Michigan": "MI",
"Minnesota": "MN",
"Mississippi": "MS",
"Missouri": "MO",
"Montana": "MT",
"Nebraska": "NE",
"Nevada": "NV",
"New Hampshire": "NH",
"New Jersey": "NJ",
"New Mexico": "NM",
"New York": "NY",
"North Carolina": "NC",
"North Dakota": "ND",
"Ohio": "OH",
"Oklahoma": "OK",
"Oregon": "OR",
"Pennsylvania": "PA",
"Rhode Island": "RI",
"South Carolina": "SC",
"South Dakota": "SD",
"Tennessee": "TN",
"Texas": "TX",
"Utah": "UT",
"Vermont": "VT",
"Virginia": "VA",
"Washington": "WA",
"West Virginia": "WV",
"Wisconsin": "WI",
"Wyoming": "WY",
"District of Columbia": "DC",
"American Samoa": "AS",
"Guam": "GU",
"Northern Mariana Islands": "MP",
"Puerto Rico": "PR",
"United States Minor Outlying Islands": "UM",
"U.S. Virgin Islands": "VI",
"AL": "AL",
"AK": "AK",
"AZ": "AZ",
"AR": "AR",
"CA": "CA",
"CO": "CO",
"CT": "CT",
"DE": "DE",
"FL": "FL",
"GA": "GA",
"HI": "HI",
"ID": "ID",
"IL": "IL",
"IN": "IN",
"IA": "IA",
"KS": "KS",
"KY": "KY",
"LA": "LA",
"ME": "ME",
"MD": "MD",
"MA": "MA",
"MI": "MI",
"MN": "MN",
"MS": "MS",
"MO": "MO",
"MT": "MT",
"NE": "NE",
"NV": "NV",
"NH": "NH",
"NJ": "NJ",
"NM": "NM",
"NY": "NY",
"NC": "NC",
"ND": "ND",
"OH": "OH",
"OK": "OK",
"OR": "OR",
"PA": "PA",
"RI": "RI",
"SC": "SC",
"SD": "SD",
"TN": "TN",
"TX": "TX",
"UT": "UT",
"VT": "VT",
"VA": "VA",
"WA": "WA",
"WV": "WV",
"WI": "WI",
"WY": "WY",
"DC": "DC",
"AS": "AS",
"GU": "GU",
"MP": "MP",
"PR": "PR",
"UM": "UM",
"VI": "VI",
}

needed this for standardizing the column where some values were full names and some were abbreviations

@shayeny
Copy link

shayeny commented Feb 17, 2022

@florent-zahoui
Copy link

thanks a lot

@bayusuarsa
Copy link

Thank you for sharing this information
@sandlerj It's worked very well, and thank you

@IMXENON
Copy link

IMXENON commented Jun 19, 2022

Thank you!

@quicksilver0
Copy link

Commenting as well, so everyone gets spam on this gist! haha

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