Python pandas - 返回指数值列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34000074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:18:49  来源:igfitidea点击:

pandas - return column of exponential values

pythonpandas

提问by Fabio Lamanna

Starting from a sample dataframe dflike:

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

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:

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

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 mathfunction to a whole column?

有没有办法将math函数应用于整列?

采纳答案by EdChum

Well math.expdoesn't understand Seriesdatatype, use numpy np.expwhich does and is vectorised so operates on the entire column:

好吧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

回答by Talis

try using:

尝试使用:

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