【问题标题】:python pandas unbound local error while calling a function 'df.apply'调用函数'df.apply'时python pandas未绑定本地错误
【发布时间】:2012-09-18 11:47:50
【问题描述】:

我正在尝试在 pandas 中使用 df.apply() 函数,但出现以下错误。如果小于“阈值”,该函数会尝试将每个条目转换为 0

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

【问题讨论】:

    标签: python pandas apply


    【解决方案1】:

    对我来说,错误消息看起来像 pandas 错误,但我认为还有另外两个问题。

    首先,我认为您必须指定命名参数或使用args 将其他参数传递给apply。您的第二个论点可能被解释为轴。但是如果你使用

    df.apply(discardValueLessThan, args=(0.1,))
    

    df.apply(discardValueLessThan, threshold=0.1)
    

    然后你会得到

    ValueError: ('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()', 'occurred at index A')
    

    因为apply 不会在元素上起作用,它会作用于整个系列对象。其他方法包括使用applymap 或布尔索引,即

    In [47]: df = DataFrame(np.random.randn(3, 3), columns=['A', 'B', 'C'])
    
    In [48]: df
    Out[48]: 
              A         B         C
    0 -0.135336 -0.274687  1.480949
    1 -1.079800 -0.618610 -0.321235
    2 -0.610420 -0.422112  0.102703
    
    In [49]: df1 = df.applymap(lambda x: discardValueLessThan(x, 0.1))
    
    In [50]: df1
    Out[50]: 
       A  B         C
    0  0  0  1.480949
    1  0  0  0.000000
    2  0  0  0.102703
    

    或者干脆

    In [51]: df[df < 0.1] = 0
    
    In [52]: df
    Out[52]: 
       A  B         C
    0  0  0  1.480949
    1  0  0  0.000000
    2  0  0  0.102703
    

    【讨论】:

    • axis 是第二个参数,因此 0.1 确实被解释为轴。如果轴不是 0 或 1,我只是推动掌握更多信息性错误消息。
    • @ChangShe:是的,我想到的错误是有人试图捕获 NameError 而不是抛出的异常。
    【解决方案2】:

    你需要这样称呼它:

    df.apply(discardValueLessThan, args=(0.1,))

    你这样做的方式 0.1 不会作为参数传递给 discardValueLessThan。

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 2016-07-05
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 2016-02-07
      • 2022-11-11
      • 1970-01-01
      相关资源
      最近更新 更多