赋值前引用的局部变量“result”
local variable 'result" referenced before assignment
我试图在 pandas Jupyter 上导入使用 spyder 创建的计算器,但收到上述错误。尝试使用 else: 但仍然出现错误。
这是我的间谍代码。
import pandas as pd
def calc_convert(Unit,result):
if Unit == 'mg/g':
testr = result*0.100
elif Unit == 'g/day':
testr = result*100
elif Unit == 'g/g':
testr = result*0.100*1000
else:
testr = result*1
return testr
这是我的 pandas jupyter 代码
lst_test = []
for index, row in test.iterrows():
lst_test.append(calc_convert(row['Unit'],row['result']))
test['converted'] = pd.Series(lst_test,index=test.index)
display(test)
这是来自 Jupyter 的回溯错误 pandas
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-170-5dfe9296da1e> in <module>
1 lst_test = []
2 for index, row in test.iterrows():
----> 3 lst_test.append(calc_convert(row['Unit'], row['result']))
4
5 test['converted'] = pd.Series(lst_test, index=test.index)
17 elif Unit == 'g/g':
18 testr = result*0.100*1000
---> 19 else:
20 testr = result*1
21
UnboundLocalError: local variable 'testr' referenced before assignment
重启内核成功。谢谢大家的意见。
我试图在 pandas Jupyter 上导入使用 spyder 创建的计算器,但收到上述错误。尝试使用 else: 但仍然出现错误。
这是我的间谍代码。
import pandas as pd
def calc_convert(Unit,result):
if Unit == 'mg/g':
testr = result*0.100
elif Unit == 'g/day':
testr = result*100
elif Unit == 'g/g':
testr = result*0.100*1000
else:
testr = result*1
return testr
这是我的 pandas jupyter 代码
lst_test = []
for index, row in test.iterrows():
lst_test.append(calc_convert(row['Unit'],row['result']))
test['converted'] = pd.Series(lst_test,index=test.index)
display(test)
这是来自 Jupyter 的回溯错误 pandas
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-170-5dfe9296da1e> in <module>
1 lst_test = []
2 for index, row in test.iterrows():
----> 3 lst_test.append(calc_convert(row['Unit'], row['result']))
4
5 test['converted'] = pd.Series(lst_test, index=test.index)
17 elif Unit == 'g/g':
18 testr = result*0.100*1000
---> 19 else:
20 testr = result*1
21
UnboundLocalError: local variable 'testr' referenced before assignment
重启内核成功。谢谢大家的意见。