Python UnboundLocalError:局部变量';说明索赔比率';分配前参考 def cal_monthly_avg(每月数据、时点数据、当前年份): st_pop_data.set_index('State',inplace=True) #将该州所有人口相加,得出最终美国人口 us_pop=st_pop_数据[‘人口]]。总和() #将州人口添加到每月的_df中 月列=月列 临时数据=局部合并(每月数据,固定数据,左指数=真,右指数=真) 对于以每月为单位的col: 每月索赔金额=临时费用[col].sum() #打印(每月总索赔) 每10万美元的索赔额=每月索赔额*100000美元/pop #打印(每10万人10万美元) 每10万人的索赔额为10万美元 #下降列总体 温度下降('总体',轴=1,就地=True) 完整的季度列表=[parameters.update\u year+'Q1',parameters.update\u year+'Q2',parameters.update\u year+'Q3',parameters.update\u year+'Q4'] 最大月份=最大(每月) 如果int(参数更新年)
这个函数保存在function.py中,然后我将这个函数导入到另一个脚本中Python UnboundLocalError:局部变量';说明索赔比率';分配前参考 def cal_monthly_avg(每月数据、时点数据、当前年份): st_pop_data.set_index('State',inplace=True) #将该州所有人口相加,得出最终美国人口 us_pop=st_pop_数据[‘人口]]。总和() #将州人口添加到每月的_df中 月列=月列 临时数据=局部合并(每月数据,固定数据,左指数=真,右指数=真) 对于以每月为单位的col: 每月索赔金额=临时费用[col].sum() #打印(每月总索赔) 每10万美元的索赔额=每月索赔额*100000美元/pop #打印(每10万人10万美元) 每10万人的索赔额为10万美元 #下降列总体 温度下降('总体',轴=1,就地=True) 完整的季度列表=[parameters.update\u year+'Q1',parameters.update\u year+'Q2',parameters.update\u year+'Q3',parameters.update\u year+'Q4'] 最大月份=最大(每月) 如果int(参数更新年),python,Python,这个函数保存在function.py中,然后我将这个函数导入到另一个脚本中 cal\u weekly\u claims.py. 然后在cal_weekly_claims.py中使用此函数: df=function.cal\u monthly\u avg(每月df、st\u pop\u数据,'2018') 我犯了个错误 错误:UnboundLocalError:赋值前引用的局部变量“state\u claim\u ratio\u df” 为什么我会犯这个错误 没有一个条件为真,因此未定义变量 这是
cal\u weekly\u claims.py.
然后在cal_weekly_claims.py中使用此函数:
df=function.cal\u monthly\u avg(每月df、st\u pop\u数据,'2018')
我犯了个错误
错误:UnboundLocalError:赋值前引用的局部变量“state\u claim\u ratio\u df”
为什么我会犯这个错误 没有一个条件为真,因此未定义变量 这是因为您没有使用正确的运算符。在Python中,布尔“and”是
和。&
运算符用于按位比较
def cal_monthly_avg(monthly_df, st_pop_data, current_year):
st_pop_data.set_index('State', inplace=True)
# Final us population by adding all population in state
us_pop = st_pop_data['Population'].sum()
# add state population into monthly_df
monthly_cols = monthly_df.columns
temp_df = pd.merge(monthly_df, st_pop_data, left_index=True, right_index=True)
for col in monthly_cols:
sum_monthly_claim = temp_df[col].sum()
# print(sum_monthly_claim)
us_per_100k_people_claims = sum_monthly_claim * 100000 / us_pop
# print(us_per_100k_people_claims)
temp_df[col] = temp_df[col] * 100000 / temp_df['Population'] / us_per_100k_people_claims
# drop column Population
temp_df.drop('Population', axis=1, inplace=True)
full_quarter_list = [parameters.update_year + 'Q1', parameters.update_year + 'Q2', parameters.update_year + 'Q3', parameters.update_year + 'Q4']
max_month = max(monthly_cols)
if int(parameters.update_year) < current_year:
temp_df[parameters.update_year + 'Q1'] = temp_df[temp_df.columns[0:3]].mean(axis=1)
temp_df[parameters.update_year + 'Q2'] = temp_df[temp_df.columns[3:6]].mean(axis=1)
temp_df[parameters.update_year + 'Q3'] = temp_df[temp_df.columns[6:9]].mean(axis=1)
temp_df[parameters.update_year + 'Q4'] = temp_df[temp_df.columns[9:12]].mean(axis=1)
state_claim_ratio_df = temp_df[full_quarter_list]
elif max_month < 12 & max_month >= 9:
part_quarter_list = [parameters.update_year + 'Q1', parameters.update_year + 'Q2', parameters.update_year + 'Q3']
temp_df[parameters.update_year + 'Q1'] = temp_df[temp_df.columns[0:3]].mean(axis=1)
temp_df[parameters.update_year + 'Q2'] = temp_df[temp_df.columns[3:6]].mean(axis=1)
temp_df[parameters.update_year + 'Q3'] = temp_df[temp_df.columns[6:9]].mean(axis=1)
state_claim_ratio_df = temp_df[part_quarter_list]
elif max_month < 9 & max_month >= 6:
part_quarter_list = [parameters.update_year + 'Q1', parameters.update_year + 'Q2']
temp_df[parameters.update_year + 'Q1'] = temp_df[temp_df.columns[0:3]].mean(axis=1)
temp_df[parameters.update_year + 'Q2'] = temp_df[temp_df.columns[3:6]].mean(axis=1)
state_claim_ratio_df = temp_df[part_quarter_list]
elif max_month < 6 & max_month >= 3:
part_quarter_list = [parameters.update_year + 'Q1']
temp_df[parameters.update_year + 'Q1'] = temp_df[temp_df.columns[0:3]].mean(axis=1)
state_claim_ratio_df = temp_df[part_quarter_list]
return state_claim_ratio_df
elif max\u month<12且max\u month>=9:
等等
但请注意,您的条件更习惯于用链式比较来表示:
elif max_month < 12 and max_month >= 9:
如果9看起来像是你忘了在函数中定义声明索赔比率df
。别忘了接受答案,作为向未来搜索者表明它解决了你的问题的标志。
if 9 <= max_month < 12: