简体   繁体   English

运行以下代码时,我收到此错误“分配前引用的局部变量 'col'”:

[英]I am getting this error “local variable 'col' referenced before assignment” when running the following code:

def log_hist(data):
    data = pd.DataFrame([])
    for col in data:
        data_log = np.log(data[col])
    return data[col].hist()




---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-127-dc391683ef40> in <module>()
----> 1 log_hist(df2)

<ipython-input-126-60d8684e3abe> in log_hist(data)
      3     for col in data:
      4         data_log = np.log(data[col])
----> 5     return data[col].hist()
      6 
      7 

UnboundLocalError: local variable 'col' referenced before assignment

Since your dataframe is empty, there is nothing to iterate through.由于您的 dataframe 是空的,因此没有什么可以迭代的。 The for loop does not instantiate the col variable. for 循环不会实例化 col 变量。 If your iterable (the dataframe) was not empty, then the variable would be available after the loop.如果您的可迭代对象(数据框)不为空,则该变量将在循环后可用。

Example of idea:想法示例:

>>> for x in range(10):
...  pass
... 
>>> x
9
>>> for y in []:
...  pass
... 
>>> y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'y' is not defined
>>> 

The variable col is only valid within the for loop.变量col仅在for循环中有效。 If you want to access the value outside of the loop (eg to capture the last value that is processed by the loop), declare a variable outside of the loop:如果要访问循环外的值(例如,捕获循环处理的最后一个值),请在循环外声明一个变量:

def log_hist(data):
    last_col = None
    for col in data:
        np.log(data[col])
        last_col = col
    return data[last_col].hist()  # will raise an exception if the dataframe is empty!

Note that I removed the line at the start of your function which zeroes out the data parameter -- I assume this was part of some debugging attempt, but it obviously completely breaks the function to have that in there.请注意,我删除了 function 开头的行,它清零了data参数——我认为这是一些调试尝试的一部分,但它显然完全破坏了 function 在那里。 :) :)

I also removed the data_log variable since it's not used anywhere;我还删除了data_log变量,因为它没有在任何地方使用; there's no point assigning a variable that you never use.分配一个你从不使用的变量是没有意义的。 (I'm not familiar with np.log , I assume it has a side-effect that is desirable -- otherwise the loop is mostly pointless to begin with and should be replaced with code that simply selects the last column without looping through the entire object.) (我不熟悉np.log ,我认为它有一个可取的副作用 - 否则循环开始时大多毫无意义,应该用只选择最后一列而不循环整个object。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到赋值之前局部变量Cruty引用的错误 - I am getting the error that the local variable cruty referenced before assignment 为什么我在赋值之前会引用局部变量? - why am I getting local variable referenced before assignment? 当我在 try-except 块中使用该变量时,我在赋值之前引用了局部变量 - i am getting local variable referenced before assignment when i use that variable in try-except block 我一直收到以下错误“分配前引用的局部变量&#39;total_results&#39;” - I keep getting the following error “local variable 'total_results' referenced before assignment” 为什么会出现错误“赋值之前引用本地变量”或“未定义全局变量”的错误? - Why am I getting either error “local variable referenced before assignment” or “global variable not defined”? 运行我的代码时出现“UnboundLocalError:在赋值之前引用了局部变量‘n’” - Getting "UnboundLocalError: local variable 'n' referenced before assignment" when running my code 我收到此错误:“UnboundLocalError:分配前引用的局部变量‘Requesting_books’” - i am getting this error : " UnboundLocalError: local variable 'Requesting_books' referenced before assignment " 我收到一个错误:“UnboundLocalError:分配前引用了局部变量'text_to_print'” - I am getting an error: “UnboundLocalError: local variable 'text_to_print' referenced before assignment” 为什么我收到错误:UnboundLocalError: local variable 'lcm' referenced before assignment - Why am I getting the error: UnboundLocalError: local variable 'lcm' referenced before assignment 为什么我会收到此错误“在赋值之前引用了局部变量‘文本’” - Why am I getting this error "local variable 'text' referenced before assignment"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM