pandas-指数值返回列

2024-05-15 02:14:32 发布

您现在位置:Python中文网/ 问答频道 /正文

从示例数据帧df开始,如:

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

我将添加一个新列,其指数值为列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函数应用于整个列?


Tags: theto数据方法format示例convertdf
2条回答

好吧math.exp不理解Series数据类型,请使用numpy^{},它可以对整个列进行矢量化,从而对整个列进行操作:

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

尝试使用:

df['exp'] = df.b.apply(np.exp)

相关问题 更多 >

    热门问题