我想在熊貓中使用df.apply()函數,但得到以下錯誤。該函數試圖每個條目轉換爲0,如果是小於「門檻」python熊貓unbound本地錯誤,同時調用函數'df.apply'
from pandas import *
import numpy as np
def discardValueLessThan(x, threshold):
if x < threshold : return 0
else: return x
df = DataFrame(np.random.randn(8, 3), columns=['A', 'B', 'C'])
>>> df
A B C
0 -1.389871 1.362458 1.531723
1 -1.200067 -1.114360 -0.020958
2 -0.064653 0.426051 1.856164
3 1.103067 0.194196 0.077709
4 2.675069 -0.848347 0.152521
5 -0.773200 -0.712175 -0.022908
6 -0.796237 0.016256 0.390068
7 -0.413894 0.190118 -0.521194
df.apply(discardValueLessThan, 0.1)
>>> df.apply(discardValueLessThan, 0.1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas-0.8.1-py2.7-macosx-10.5-x86_64.egg/pandas/core/frame.py", line 3576, in apply
return self._apply_standard(f, axis)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas-0.8.1-py2.7-macosx-10.5-x86_64.egg/pandas/core/frame.py", line 3637, in _apply_standard
e.args = e.args + ('occurred at index %s' % str(k),)
UnboundLocalError: local variable 'k' referenced before assignment
軸線是如此0.1確實被解釋爲軸心的第二參數。如果axis不是0或1,我只是推動掌握更多信息的錯誤消息。 –
@ChangShe:是的,我想到的錯誤是有人試圖捕獲NameError,當這不是將拋出的異常。 – DSM