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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With