Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:18:25+00:00 2026-06-12T00:18:25+00:00

I am trying to use df.apply() function in pandas but getting the following error.

  • 0

I am trying to use df.apply() function in pandas but getting the following error. The function is trying to convert every entry into 0 if it is less than ‘threshold’

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:18:26+00:00Added an answer on June 12, 2026 at 12:18 am

    The error message looks like a pandas bug to me, but I think there are two other problems.

    First, I think you have to either specify named parameters or use args to pass additional arguments to apply. Your second argument is probably being interpreted as an axis. But if you use

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

    or

    df.apply(discardValueLessThan, threshold=0.1)
    

    then you’ll get

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

    because apply doesn’t act elementwise, it acts on entire Series objects. Other approaches include using applymap or boolean indexing, i.e.

    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
    

    or simply

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to use CIColorMap filter, but getting run-time error [NSImage _feImage]: unrecognized selector sent
I'm trying to wrap my head around Javascript's function.apply(). You use it like this:
I'm getting the error Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type when I
I'm trying to use CROSS APPLY in SQL, but only want to use the
I'm trying to use MySQL and ebeans for saving my tasks. When i apply
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I'am trying to use some values from the upper function in the inner function:
I'm trying to use a raphael.js example located here: http://raphaeljs.com/pie.html but I want to
I am working with xslt 1.0 and trying to use the XSLT document function
I'm trying to use callLater with FlexUnit v0.9: public function testCallLater():void { Application.application.callLater( addAsync(function():void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.