pandas -指数值的返回列 [英] pandas - return column of exponential values
本文介绍了 pandas -指数值的返回列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从示例数据帧df
开始,例如:
Starting from a sample dataframe df
like:
a,b
0,0.71
1,0.75
2,0.80
3,0.90
我将添加具有列b
的指数值的新列.到目前为止,我尝试过:
I would add a new column with exponential values of column b
. So far I tried:
df['exp'] = math.exp(df['b'])
但是此方法返回:
"cannot convert the series to {0}".format(str(converter)"
TypeError: cannot convert the series to <type 'float'>
是否可以将math
函数应用于整个列?
Is there a way to apply a math
function to a whole column?
推荐答案
math.exp
无法理解Series
数据类型,请使用numpy
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
这篇关于 pandas -指数值的返回列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文