python - Applying functions to a pandas DataFrame gives Value Error (just one argument) -


it seems can apply functions without problems dataframe, other give value error.

dates = pd.date_range('20130101',periods=6) data = np.random.randn(6,4)  df = pd.dataframe(data,index=dates,columns=list('abcd'))  def my_max(y):     return max(y,0)  def times_ten(y):     return 10*y  df.apply(lambda x:times_ten(x)) # works fine df.apply(lambda x:my_max(x)) # doesn't work 

the first apply works fine, second 1 generates a:

valueerror: ('the truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred @ index a')

i know can generate "max(df,0)" in other ways (e.g. df[df<0]=0), i'm not looking solution particular problem. rather, i'm interested in why apply above doesn't work.

max cannot handle scalar , array:

>>> max(df['a'], 0) valueerror: truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all(). 

either use np.maximum element-wise maximum:

>>> def my_max(y): ...     return np.maximum(y, 0) ...  >>> df.apply(lambda x:my_max(x))                      b      c      d 2013-01-01  0.000  0.000  0.178  0.992 2013-01-02  0.000  1.060  0.000  0.000 2013-01-03  0.528  2.408  2.679  0.000 2013-01-04  0.564  0.573  0.320  1.220 2013-01-05  0.903  0.497  0.000  0.032 2013-01-06  0.505  0.000  0.000  0.000 

or use .applymap operates elementwise:

>>> def my_max(y): ...     return max(y,0) ...  >>> df.applymap(lambda x:my_max(x))                      b      c      d 2013-01-01  0.000  0.000  0.178  0.992 2013-01-02  0.000  1.060  0.000  0.000 2013-01-03  0.528  2.408  2.679  0.000 2013-01-04  0.564  0.573  0.320  1.220 2013-01-05  0.903  0.497  0.000  0.032 2013-01-06  0.505  0.000  0.000  0.000 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -