熊猫-指数值的返回列


问题内容

从以下示例数据帧开始df

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

我将添加一个具有column指数值的新列b。到目前为止,我尝试了:

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

但是此方法返回:

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

有没有一种方法可以将math函数应用于整个列?


问题答案:

好吧math.exp,我不理解Series数据类型,使用numpynp.exp可以并且将其向量化,因此可以对整个列进行操作:

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