从示例数据框开始,df如:
a,b
0,0.71
1,0.75
2,0.80
3,0.90
Run Code Online (Sandbox Code Playgroud)
我会添加一个具有指数值列的新列b.到目前为止我试过:
df['exp'] = math.exp(df['b'])
Run Code Online (Sandbox Code Playgroud)
但是这个方法返回:
"cannot convert the series to {0}".format(str(converter)"
TypeError: cannot convert the series to <type 'float'>
Run Code Online (Sandbox Code Playgroud)
有没有办法将math函数应用于整个列?
EdC*_*ica 16
嗯math.exp,不理解Series数据类型,使用numpy np.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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15853 次 |
| 最近记录: |