steff
steff

Reputation: 906

Need to add a column to a pd.df with values from a function

My Dataframe looks like this:

In [325]: TYVOL.tail()
Out[325]: 
               Close
Date                
2017-11-24  0.027705
2017-11-27  0.029335
2017-11-28  0.029335
2017-11-29  0.029498
2017-11-30  0.031454

tried this:

TYVOL['pb'] = [my_probability_gamma(TYVOL.Close[date],shape0,shape1,shape2,scale0,
               scale1,scale2,pbv0,pbv1,pbv2,date) for date in TYVOL.index]

which throws a KeyError: Timestamp

Anything obvious i am doing wrong? Thanks for your help.

Upvotes: 2

Views: 47

Answers (1)

jezrael
jezrael

Reputation: 863166

I think you need loc:

TYVOL['pb'] = [my_probability_gamma(TYVOL.loc[date, 'Close'],shape0,shape1,shape2,scale0,
               scale1,scale2,pbv0,pbv1,pbv2,date) for date in TYVOL.index]

Or apply and instead date use x.name:

f = lambda x: my_probability_gamma(x['Close'],shape0,shape1,shape2,
                                   scale0, scale1,scale2,pbv0,pbv1,pbv2,x.name)
TYVOL['pb'] = TYVOL.apply(f, axis=1)

Or use iteritems:

TYVOL['pb'] = [my_probability_gamma(c,shape0,shape1,shape2,scale0,
               scale1,scale2,pbv0,pbv1,pbv2,d) for d, c in TYVOL['Close'].iteritems()]

Test:

def my_probability_gamma(x,y,z):
    return (x,y,z)

shape0 = 1

TYVOL['pb'] = [my_probability_gamma(TYVOL.loc[date, 'Close'],shape0,date) for date in TYVOL.index]
print (TYVOL)
               Close                                  pb
Date                                                    
2017-11-24  0.027705  (0.027705, 1, 2017-11-24 00:00:00)
2017-11-27  0.029335  (0.029335, 1, 2017-11-27 00:00:00)
2017-11-28  0.029335  (0.029335, 1, 2017-11-28 00:00:00)
2017-11-29  0.029498  (0.029498, 1, 2017-11-29 00:00:00)
2017-11-30  0.031454  (0.031454, 1, 2017-11-30 00:00:00)

shape0 = 1
 f = lambda x: my_probability_gamma(x['Close'],shape0,x.name)
TYVOL['pb'] = TYVOL.apply(f, axis=1)
print (TYVOL)
               Close                                            pb
Date                                                              
2017-11-24  0.027705  (0.027705, 1, 2017-11-24T00:00:00.000000000)
2017-11-27  0.029335  (0.029335, 1, 2017-11-27T00:00:00.000000000)
2017-11-28  0.029335  (0.029335, 1, 2017-11-28T00:00:00.000000000)
2017-11-29  0.029498  (0.029498, 1, 2017-11-29T00:00:00.000000000)
2017-11-30  0.031454  (0.031454, 1, 2017-11-30T00:00:00.000000000)

TYVOL['pb'] = [my_probability_gamma(c,shape0,d) for d, c in TYVOL['Close'].iteritems()]
print (TYVOL)
               Close                                              pb
Date                                                                
2017-11-24  0.027705              (0.027705, 1, 2017-11-24 00:00:00)
2017-11-27  0.029335  (0.029335000000000003, 1, 2017-11-27 00:00:00)
2017-11-28  0.029335  (0.029335000000000003, 1, 2017-11-28 00:00:00)
2017-11-29  0.029498  (0.029498000000000003, 1, 2017-11-29 00:00:00)
2017-11-30  0.031454              (0.031454, 1, 2017-11-30 00:00:00)

Upvotes: 2

Related Questions