我试图通过在列表中的每一项后面添加一个数字来从列表中创建新的列,但是出现了这个错误。你知道吗
我试图将列表声明为“全局变量”,但它不起作用。你知道吗
colunasPes = ['numeroOrdem', 'nome']
df = pd.DataFrame(columns = colunasFam+colunasPes, index = range(len(lista)))
def criaColunas(i):#cria novas colunas no df para um novo membro
for i in len(colunasPes):
i = str(i)
colunasNova[i] = colunasPes[i]+i
colunasPes = colunasPes + colunasNova
df = pd.DataFrame(columns = colunasFam+colunasPes, index = range(len(lista)))
criaColunas(1)
但我得到了一个错误:
UnboundLocalError: local variable 'colunasPes' referenced before assignment
colunasPes = colunasPes + colunasNova
在函数的作用域中引入了一个新变量,也称为colunasPes
,因此在赋值之前要引用它您可以在函数中将它标记为
global
。你知道吗还是传过来?你知道吗
顺便说一句-您正在传入
i
,然后在for
循环中更改它。这是故意的吗?你知道吗此外,您似乎试图更改函数中不在范围内的另一个变量。你知道吗
尝试返回数据帧?你知道吗
相关问题 更多 >
编程相关推荐