我正在为每个州分配区域。我的代码从excel文件中读取,大约有3万行。我设置了一个字典,将每个状态分配给一个区域,并将状态缩写分配给每个状态名称。我正在尝试创建一列以填充每个行项目的区域,但始终在'MA'处出现KeyError(因为我的Excel文件中没有此状态的行项目)。
我尝试使用'except'以及'if missing'编写短语,但似乎都无法清除错误并产生所需的结果。我也尝试从字典中删除MA,但是出现相同的错误。我是Python的新手,我确定这里有一个简单的修复程序,但不知道它是什么。
states = {
'AK': 'Alaska',
'AL': 'Alabama',
'AR': 'Arkansas',
'AZ': 'Arizona',
'CA': 'California',
'CO': 'Colorado',
'CT': 'Connecticut',
'DC': 'District of Columbia',
'DE': 'Delaware',
'FL': 'Florida',
'GA': 'Georgia',
'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',
'MS': 'Mississippi',
'MT': 'Montana',
'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',
'RI': 'Rhode Island',
'SC': 'South Carolina',
'SD': 'South Dakota',
'TN': 'Tennessee',
'TX': 'Texas',
'UT': 'Utah',
'VA': 'Virginia',
'VT': 'Vermont',
'WA': 'Washington',
'WI': 'Wisconsin',
'WV': 'West Virginia',
'WY': 'Wyoming'
}
stateplusdc = states.keys()
state_abbrev = {v: k for k, v in states.items()}
state_code = {
'AK': '10','AL': '4', 'AR': '9', 'AR': '6', 'CA': '9', 'CO': '8',
'CT': '1', 'DC': '3', 'DE': '3', 'FL': '4', 'GA': '4', 'HI': '9', 'IA': '7', 'ID': '10', 'IL': '5', 'IN': '5', 'KS': '7', 'KY': '4', 'LA': '6', 'MA': '1', 'MD': '3', 'ME': '1', 'MI': '5', 'MN': '5','MO': '7', 'MS': '4', 'MT': '8', 'NC': '4', 'ND': '8', 'NE': '7', 'NH': '1', 'NJ': '2', 'NM': '6','NV': '9', 'NY': '2', 'OH': '5', 'OK': '6','OR': '10', 'PA': '3', 'PR': '2', 'RI': '1', 'SC': '4', 'SD': '8', 'TN': '4', 'TX': '6', 'UT': '8', 'VA': '3', 'VI': '2', 'VT': '1', 'WA': '10', 'WI': '5', 'WV': '3', 'WY': '8', 'PI': '9'
}
state_region = {v: k for k, v in state_code.items()}
excel_file = r'/Users/amandawhiting/Desktop/PA_spending_excel.xlsx'
df = pd.read_excel(excel_file)
df = df.rename(columns={'DAMAGE_CATEGORY_CODE': 'damageCode', 'FEDERAL_SHARE_OBLIGATED':'FedShareObligated', 'PROJECT_AMOUNT': 'ProjectAmount'})
df = df[df['FedShareObligated']>= 0]
df = df[df['ProjectAmount'] >= 0df2 = pd.read_csv("/Users/amandawhiting/Desktop/DisasterDeclarationsSummaries.csv", usecols = ['disasterNumber', 'fyDeclared', 'state'])
df = df[df['damageCode'] != 'A - Debris Removal']
df = df[df['damageCode'] != 'B - Protective Measures']
df = df[df['damageCode'] != 'Z - State Management']
df = df[df['damageCode'] != 'H - Fire Management']
df = df.drop_duplicates()
df = df.reset_index(drop=True)
df2 = pd.read_csv("/Users/amandawhiting/Desktop/DisasterDeclarationsSummaries.csv", usecols = ['disasterNumber', 'fyDeclared', 'state'])
df2 = df2[df2['fyDeclared'] > 1991]
df2 = df2[df2['fyDeclared'] < 2017]
df2 = df2.reset_index(drop=True)
df2['disasterNumber'] = df2['disasterNumber'].astype(int)
fulldf = pd.merge(df, df2, left_on = 'DISASTER_NUMBER', right_on = 'disasterNumber', how = 'inner',)
fulldf = fulldf.drop_duplicates()
fulldf = fulldf.reset_index(drop=True)
def get_region():
return [state_region[i] for i in fulldf['state']]
fulldf["Region"] = get_region()
fulldf.head()
预期结果:现有表中标有“地区”的新列,该列将在该行中填充该州相应区域的每个单元格。
实际结果:关键错误“ MA”:
KeyError Traceback (most recent call last)
<ipython-input-403-13becd272809> in <module>
31 return [state_region[i] for i in fulldf['state']]
32
---> 33 fulldf["Region"] = get_region()
34
35 fulldf.head()
<ipython-input-403-13becd272809> in get_region()
29
30 def get_region():
---> 31 return [state_region[i] for i in fulldf['state']]
32
33 fulldf["Region"] = get_region()
<ipython-input-403-13becd272809> in <listcomp>(.0)
29
30 def get_region():
---> 31 return [state_region[i] for i in fulldf['state']]
32
33 fulldf["Region"] = get_region()
KeyError: 'MA'
答案 0 :(得分:0)
尝试一下:
state_region = {v: k for k, v in state_code.items()}
def get_region():
result = []
for i in fulldf['state'] :
if i in state_region :
result.append(state_region[i])
else :
result.append("NA")
return result
答案 1 :(得分:0)
IIUC,您正在寻找其中一个:
# replace non-available keys with NA
fulldf["Region"] = fulldf['state'].map(state_region)
或
# keep the non-available keys intact
fulldf["Region"] = fulldf['state'].replace(state_region)