-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
78 lines (62 loc) · 3.04 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import re
import pandas as pd
def make_dict_of_state_to_lowercase_and_no_space():
states = [
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Dist. of Col.', 'Florida',
'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa',
'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland',
'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
'Missouri', 'Montana', 'Nebraska', 'CD-1', 'CD-2', 'CD-3',
'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
'West Virginia', 'Wisconsin', 'Wyoming',
]
new_names = map(lambda x: x.lower(), states)
regex = re.compile('[^a-zA-Z]')
new_names = map(lambda x: regex.sub('', x), new_names)
return dict(zip(new_names, states))
def make_state_to_lower_dict():
states = [
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Dist. of Col.', 'Florida',
'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa',
'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland',
'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
'Missouri', 'Montana', 'Nebraska', 'CD-1', 'CD-2', 'CD-3',
'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
'West Virginia', 'Wisconsin', 'Wyoming',
]
new_names = map(lambda x: x.lower(), states)
regex = re.compile('[^a-zA-Z]')
new_names = map(lambda x: regex.sub('', x), new_names)
return dict(zip(states, new_names))
def translate_dictionary_to_full_statename(dictionary={}):
full_name = make_dict_of_state_to_lowercase_and_no_space()
new_dictionary = {}
for name, value in dictionary.iteritems():
if name in full_name:
new_dictionary[full_name[name]] = value
return new_dictionary
def get_state_neighbors(avoid=["AK", "DC"]):
codes_df = pd.read_csv("data/us_states_and_code.csv")
neigh_df = pd.read_csv("data/neighbors-states.csv")
codes_dict = dict(zip(codes_df['Abbreviation'].values,
codes_df['State'].values))
neigh_dict = {}
for abbr in codes_df["Abbreviation"].values:
if abbr not in avoid:
neighbors = ["".join(row).replace(abbr, "") for row in neigh_df.values if abbr in row]
neighbors = [codes_dict[x] for x in neighbors if x not in avoid]
neigh_dict[codes_dict[abbr]] = neighbors
return neigh_dict
def to_lower_letters(text=""):
new_text = text.lower()
regex = re.compile('[^a-zA-Z]')
new_text = regex.sub('', new_text)
return new_text