python 通过 python dict 和 re.sub 缩写的子状态名称
python sub state names for abbrev via python dict with re.sub
我有一个数据框,其中有一列包含州名。这些名称混合了官方缩写和部分拼写以及完整的州名。
d = pd.DataFrame(['fla', 'fl', 'del', 'ohio', 'calif', 'ca', 'del', 'texas', 'miss', 'tx', 'new mex'],
columns = ["state"])
这里有一个 python 带有州缩写和名称的字典:https://code.activestate.com/recipes/577305-python-dictionary-of-us-states-and-territories/
我想查看数据框 d
并在 dict
中找到最佳匹配并替换 d['state']
中的值。我不认为我想使用 replace
因为我想替换“整个单词”而不是子字符串。期望的结果:
d = ['fl', 'fl', 'de', 'oh', 'ca', 'ca', 'de', 'tx', 'ms', 'tx', 'nm']
将字典直接加载到我的控制台,并调用它 states_dict
,我尝试了以下方法(基于此 )
d['state'] = d['state'].map(states_dict)
为我的数据框中的每个条目生成 nan
,d
。
如有任何帮助,我们将不胜感激。
谢谢。
这似乎可行:通过从 d['state']
中获取每个值并在每个字母之间添加 \w*
并使用 match
从每个字典的开头搜索来搜索州名值以不区分大小写的方式。一旦找到,return 找到的值小写。
而且我认为miss
必须是mo
,而不是mx
。
import pandas as pd
import re
d = pd.DataFrame(['fla', 'fl', 'del', 'ohio', 'calif', 'ca', 'del', 'texas', 'miss', 'tx', 'new mex', 'NY', 'NJ', 'NM', 'NC'], columns = ["state"])
states = {
'AK': 'Alaska',
'AL': 'Alabama',
'AR': 'Arkansas',
'AS': 'American Samoa',
'AZ': 'Arizona',
'CA': 'California',
'CO': 'Colorado',
'CT': 'Connecticut',
'DC': 'District of Columbia',
'DE': 'Delaware',
'FL': 'Florida',
'GA': 'Georgia',
'GU': 'Guam',
'HI': 'Hawaii',
'IA': 'Iowa',
'ID': 'Idaho',
'IL': 'Illinois',
'IN': 'Indiana',
'KS': 'Kansas',
'KY': 'Kentucky',
'LA': 'Louisiana',
'MA': 'Massachusetts',
'MD': 'Maryland',
'ME': 'Maine',
'MI': 'Michigan',
'MN': 'Minnesota',
'MO': 'Missouri',
'MP': 'Northern Mariana Islands',
'MS': 'Mississippi',
'MT': 'Montana',
'NA': 'National',
'NC': 'North Carolina',
'ND': 'North Dakota',
'NE': 'Nebraska',
'NH': 'New Hampshire',
'NJ': 'New Jersey',
'NM': 'New Mexico',
'NV': 'Nevada',
'NY': 'New York',
'OH': 'Ohio',
'OK': 'Oklahoma',
'OR': 'Oregon',
'PA': 'Pennsylvania',
'PR': 'Puerto Rico',
'RI': 'Rhode Island',
'SC': 'South Carolina',
'SD': 'South Dakota',
'TN': 'Tennessee',
'TX': 'Texas',
'UT': 'Utah',
'VA': 'Virginia',
'VI': 'Virgin Islands',
'VT': 'Vermont',
'WA': 'Washington',
'WI': 'Wisconsin',
'WV': 'West Virginia',
'WY': 'Wyoming'
}
def best_match(x):
if len(x) == 2: # Try another way for 2-letter codes
for a,n in states.items():
if len(n.split()) == 2:
if "".join([c[0] for c in n.split()]).lower() == x.lower():
return a.lower()
new_rx = re.compile(r"\w*".join([ch for ch in x]), re.I)
for a,n in states.items():
if new_rx.match(n):
return a.lower()
d['state_corrected'] = d['state'].apply(lambda x: best_match(x))
结果:
state state_corrected
0 fla fl
1 fl fl
2 del de
3 ohio oh
4 calif ca
5 ca ca
6 del de
7 texas tx
8 miss mo
9 tx tx
10 new mex nm
11 NY ny
12 NJ nj
13 NM nm
14 NC nc
我有一个数据框,其中有一列包含州名。这些名称混合了官方缩写和部分拼写以及完整的州名。
d = pd.DataFrame(['fla', 'fl', 'del', 'ohio', 'calif', 'ca', 'del', 'texas', 'miss', 'tx', 'new mex'],
columns = ["state"])
这里有一个 python 带有州缩写和名称的字典:https://code.activestate.com/recipes/577305-python-dictionary-of-us-states-and-territories/
我想查看数据框 d
并在 dict
中找到最佳匹配并替换 d['state']
中的值。我不认为我想使用 replace
因为我想替换“整个单词”而不是子字符串。期望的结果:
d = ['fl', 'fl', 'de', 'oh', 'ca', 'ca', 'de', 'tx', 'ms', 'tx', 'nm']
将字典直接加载到我的控制台,并调用它 states_dict
,我尝试了以下方法(基于此
d['state'] = d['state'].map(states_dict)
为我的数据框中的每个条目生成 nan
,d
。
如有任何帮助,我们将不胜感激。
谢谢。
这似乎可行:通过从 d['state']
中获取每个值并在每个字母之间添加 \w*
并使用 match
从每个字典的开头搜索来搜索州名值以不区分大小写的方式。一旦找到,return 找到的值小写。
而且我认为miss
必须是mo
,而不是mx
。
import pandas as pd
import re
d = pd.DataFrame(['fla', 'fl', 'del', 'ohio', 'calif', 'ca', 'del', 'texas', 'miss', 'tx', 'new mex', 'NY', 'NJ', 'NM', 'NC'], columns = ["state"])
states = {
'AK': 'Alaska',
'AL': 'Alabama',
'AR': 'Arkansas',
'AS': 'American Samoa',
'AZ': 'Arizona',
'CA': 'California',
'CO': 'Colorado',
'CT': 'Connecticut',
'DC': 'District of Columbia',
'DE': 'Delaware',
'FL': 'Florida',
'GA': 'Georgia',
'GU': 'Guam',
'HI': 'Hawaii',
'IA': 'Iowa',
'ID': 'Idaho',
'IL': 'Illinois',
'IN': 'Indiana',
'KS': 'Kansas',
'KY': 'Kentucky',
'LA': 'Louisiana',
'MA': 'Massachusetts',
'MD': 'Maryland',
'ME': 'Maine',
'MI': 'Michigan',
'MN': 'Minnesota',
'MO': 'Missouri',
'MP': 'Northern Mariana Islands',
'MS': 'Mississippi',
'MT': 'Montana',
'NA': 'National',
'NC': 'North Carolina',
'ND': 'North Dakota',
'NE': 'Nebraska',
'NH': 'New Hampshire',
'NJ': 'New Jersey',
'NM': 'New Mexico',
'NV': 'Nevada',
'NY': 'New York',
'OH': 'Ohio',
'OK': 'Oklahoma',
'OR': 'Oregon',
'PA': 'Pennsylvania',
'PR': 'Puerto Rico',
'RI': 'Rhode Island',
'SC': 'South Carolina',
'SD': 'South Dakota',
'TN': 'Tennessee',
'TX': 'Texas',
'UT': 'Utah',
'VA': 'Virginia',
'VI': 'Virgin Islands',
'VT': 'Vermont',
'WA': 'Washington',
'WI': 'Wisconsin',
'WV': 'West Virginia',
'WY': 'Wyoming'
}
def best_match(x):
if len(x) == 2: # Try another way for 2-letter codes
for a,n in states.items():
if len(n.split()) == 2:
if "".join([c[0] for c in n.split()]).lower() == x.lower():
return a.lower()
new_rx = re.compile(r"\w*".join([ch for ch in x]), re.I)
for a,n in states.items():
if new_rx.match(n):
return a.lower()
d['state_corrected'] = d['state'].apply(lambda x: best_match(x))
结果:
state state_corrected
0 fla fl
1 fl fl
2 del de
3 ohio oh
4 calif ca
5 ca ca
6 del de
7 texas tx
8 miss mo
9 tx tx
10 new mex nm
11 NY ny
12 NJ nj
13 NM nm
14 NC nc