Home > Blockchain >  Replacing abbreviated states in df series with full state with list comprehension
Replacing abbreviated states in df series with full state with list comprehension

Time:12-03

I feel like this is possible but I'm not sure:

This is my code right now:

def full_states(upload_list):
    abr_states = ["AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY", "RI", "ON", "AB", "BC", "DC", "NB"]
    state_names = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming", "Rhode Island", "Ontario", "Alberta", "British Columbia", "Washington D.C.", "New Brunswick"]
    series = []
    for a in range(len(abr_states)):
                for s in range(len(state_names)):
                    upload_series = upload_list["State"].str.replace(abr_states[a],state_names[s])
                    series.append(upload_series)
    print(series)
    upload_list["State"] = series
    return upload_list

full_states(upload_list)

If it's not obvious I have a pandas dataframe column that has abbreviated states in it. I am attempting to loop through a list of abbreviated states and one of full states and insert them into the str.replace pandas series method to replace the abbreviated states in the column (all are abreviated currently) with the full state names. I can't tell what I should be doing differently to get the desired result and I'm stuck. Any direction appreciated!

CodePudding user response:

Here is a long dictionary of two letter postal designations and their corresponding state/province/area.

abbrevs = {
    'AA': 'Armed Forces Americas',
    'AB': 'Alberta',
    'AE': 'Armed Forces Europe',
    'AK': 'Alaska',
    'AL': 'Alabama',
    'AP': 'Armed Forces Pacific',
    'AR': 'Arkansas',
    'AS': 'American Samoa',
    'AZ': 'Arizona',
    'BC': 'British Columbia',
    'CA': 'California',
    'CD': 'Canada',
    'CO': 'Colorado',
    'CT': 'Connecticut',
    'DC': 'Dist. Of Columbia',
    'DE': 'Delaware',
    'FF': 'Foreign Countries',
    'FL': 'Florida',
    'GA': 'Georgia',
    'GU': 'Guam',
    'HI': 'Hawaii',
    'IA': 'Iowa',
    'ID': 'Idaho',
    'IL': 'Illinois',
    'IN': 'Indiana',
    'KS': 'Kansas',
    'KY': 'Kentucky',
    'LA': 'Louisiana',
    'MA': 'Massachusetts',
    'MB': 'Manitoba',
    'MD': 'Maryland',
    'ME': 'Maine',
    'MI': 'Michigan',
    'MN': 'Minnesota',
    'MO': 'Missouri',
    'MS': 'Mississippi',
    'MT': 'Montana',
    'MX': 'Mexico',
    'NB': 'New Brunswick',
    'NC': 'North Carolina',
    'ND': 'North Dakota',
    'NE': 'Nebraska',
    'NH': 'New Hampshire',
    'NJ': 'New Jersey',
    'NL': 'Newfoundland and Labrador',
    'NM': 'New Mexico',
    'NS': 'Nova Scotia',
    'NT': 'Northwest Territories',
    'NU': 'Nunavut',
    'NV': 'Nevada',
    'NY': 'New York',
    'OH': 'Ohio',
    'OK': 'Oklahoma',
    'ON': 'Ontario',
    'OR': 'Oregon',
    'PA': 'Pennsylvania',
    'PE': 'Prince Edward Island',
    'PR': 'Puerto Rico',
    'QC': 'Quebec',
    'RI': 'Rhode Island',
    'SC': 'South Carolina',
    'SD': 'South Dakota',
    'SK': 'Saskatchewan',
    'TN': 'Tennessee',
    'TT': 'Trust Territory',
    'TX': 'Texas',
    'UN': 'Unknown',
    'UT': 'Utah',
    'VA': 'Virginia',
    'VI': 'Virgin Islands',
    'VT': 'Vermont',
    'WA': 'Washington',
    'WI': 'Wisconsin',
    'WV': 'West Virginia',
    'WY': 'Wyoming',
    'YT': 'Yukon ',
}

If you have a Series/DataFrame that contains these codes, you can replace them using:

upload_list.State.replace(abbrevs, inplace=True)
  • Related