[英]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.