即使将'i'声明为全局变量,它仍显示'i'未定义
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了即使将'i'声明为全局变量,它仍显示'i'未定义相关的知识,希望对你有一定的参考价值。
i = 0
def func(x):
global i
sum1 = 0
for ele in x:
minus = ele - data['Mean'][i]
sum1 += np.square(minus)
i += 1
return sum1
data['Std'] = Top15_new.groupby('Continents').agg({"Population":func})
return data
answer_eleven()
NameError: name 'i' is not defined
答案
由于未提供数据,所以我组成了DataFrame和数据。我只是缩进您的代码,如下所示:
i = 0
def func(x):
global i
sum1 = 0
for ele in x:
minus = ele - data['Mean'][i]
sum1 += np.square(minus)
i += 1
return sum1
data['Std'] = Top15_new.groupby('Continents').agg({"Population":func})
data
这里是上面代码的输出,没有错误。再次将数据组成数据
Mean Std
0 2 NaN
1 3 NaN
2 4 1.0
3 5 5.0
4 6 NaN
以上是关于即使将'i'声明为全局变量,它仍显示'i'未定义的主要内容,如果未能解决你的问题,请参考以下文章