0

So I created a dictionary with every state abbreviation and the keys are "East", "Central", and "West"

states_dict = {
    'West': ["CA","OR","WA","NV","ID","UT","AZ","MT","AK"],
    'Central': ["WY","CO","NM","ND","SD","NE","KS","OK","TX","MN","IA","MO","AR","LA","WI","IL","MS"],
    'East': ["MI","IN","KY","TN","AL","OH","GA","FL","SC","NC","VA","WV","DE","MD","NJ","PA","NY","CT","RI","MA","VT","NH","ME"] 
}

I'm trying to figure out a way to apply this to a column of code and return the dictionary keys instead in the column. For example if it is ["CA", "FL"] I want the column to be ["West","East]. I created a for loop where i appended the keys into a new list and it wasnt the correct length so I started using .replace and that is just continually running. Here is my code.

for x in df['X20']:
    for i,j in states_dict.items():
        for v in j:
            if x==v:
                x20=df['X20'].replace(x,i)

            else:
                pass
x20
Glory
  • 11
  • 2
  • Does this answer your question? [switch key and values in a dict of lists](https://stackoverflow.com/questions/54422204/switch-key-and-values-in-a-dict-of-lists) – metatoaster Feb 19 '20 at 05:13

3 Answers3

0

Not sure if you need columns, but it's easy enough to inverse your keys/values to easily extract the data you're looking for:

keyed_by_region = {
    'West': ["CA","OR","WA","NV","ID","UT","AZ","MT","AK"],
    'Central': ["WY","CO","NM","ND","SD","NE","KS","OK","TX","MN","IA","MO","AR","LA","WI","IL","MS"],
    'East': ["MI","IN","KY","TN","AL","OH","GA","FL","SC","NC","VA","WV","DE","MD","NJ","PA","NY","CT","RI","MA","VT","NH","ME"] 
}
​
keyed_by_states = {}
for region, states in keyed_by_region.items():
    for state in states:
        keyed_by_states[state] = region
        
print('FL', keyed_by_states['FL'])
print('CA', keyed_by_states['CA'])

FL East

CA West

Community
  • 1
  • 1
monkut
  • 42,176
  • 24
  • 124
  • 155
0

You can do a list comprehension that searches for keys that contain search fields in their values:

states_dict = {
    'West': ["CA","OR","WA","NV","ID","UT","AZ","MT","AK"],
    'Central': ["WY","CO","NM","ND","SD","NE","KS","OK","TX","MN","IA","MO","AR","LA","WI","IL","MS"],
    'East': ["MI","IN","KY","TN","AL","OH","GA","FL","SC","NC","VA","WV","DE","MD","NJ","PA","NY","CT","RI","MA","VT","NH","ME"] 
}

search = ["CA", "FL"] 
print([k for k, v in states_dict.items() if any(y in v for y in search)])
# ['West', 'East']
Austin
  • 25,759
  • 4
  • 25
  • 48
  • What the sense of using ``any()`` method here? If it's can be just as: ``[ k for k, v in states_dict.items() for y in search if y in v]`` – IVI Feb 19 '20 at 15:25
  • @IVI, You won't have states repeating. – Austin Feb 19 '20 at 15:56
  • It's good for duplication as: ``["CA", "FL", "CA"]``, but it also will skip this example ``["CA", "FL", "IN"]`` and output will be still ``['West', 'East']`` – IVI Feb 19 '20 at 16:14
  • @IVI, Exactly. Let OP intervene if he is anticipating different. – Austin Feb 19 '20 at 17:00
0

First instead of looping through the whole dictionary (using states_dict.items()) use the key to find the value as looping destroys the whole purpose of using a dictionary.

i.e. you should instead use

.replace(x,states_dic[<state code>])

replace the with the variable that stands for the state code

but for that you must remake your dictionary as

{<state code>:<east/west/north/south>}

for example:

new_dict = {'CA':'west','OR':'west'...}

you can do so by using your existing dictionary

new_dict = {}
for key, value in states_dict.items():
    for states in value:
         new_dict.update({states:key})
kinshukdua
  • 1,944
  • 1
  • 5
  • 15