Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas - return column of exponential values

Tags:

python

pandas

Starting from a sample dataframe df like:

a,b
0,0.71
1,0.75
2,0.80
3,0.90

I would add a new column with exponential values of column b. So far I tried:

df['exp'] = math.exp(df['b'])

but this method returns:

"cannot convert the series to {0}".format(str(converter)"
TypeError: cannot convert the series to <type 'float'>

Is there a way to apply a math function to a whole column?

like image 914
Fabio Lamanna Avatar asked Nov 30 '15 13:11

Fabio Lamanna


1 Answers

Well math.exp doesn't understand Series datatype, use numpy np.exp which does and is vectorised so operates on the entire column:

In [24]:
df['exp'] = np.exp(df['b'])
df

Out[24]:
   a     b       exp
0  0  0.71  2.033991
1  1  0.75  2.117000
2  2  0.80  2.225541
3  3  0.90  2.459603
like image 171
EdChum Avatar answered Oct 16 '22 21:10

EdChum