Python pandas-指数值的返回列
从示例数据帧开始,如:Python pandas-指数值的返回列,python,pandas,Python,Pandas,从示例数据帧开始,如: a,b 0,0.71 1,0.75 2,0.80 3,0.90 我将添加一个新列,其指数值为columnb。到目前为止,我试过: df['exp'] = math.exp(df['b']) 但此方法返回: "cannot convert the series to {0}".format(str(converter)" TypeError: cannot convert the series to <type 'float'> “无法将序列转换为{0}”
a,b
0,0.71
1,0.75
2,0.80
3,0.90
我将添加一个新列,其指数值为columnb
。到目前为止,我试过:
df['exp'] = math.exp(df['b'])
但此方法返回:
"cannot convert the series to {0}".format(str(converter)"
TypeError: cannot convert the series to <type 'float'>
“无法将序列转换为{0}”。格式(str(转换器)”
TypeError:无法将序列转换为
有没有一种方法可以将
math
函数应用于整个列?好的math.exp
不理解系列
数据类型,请使用numpy,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)
这确实有效,尽管它比仅使用Series作为参数调用np.exp要简单一些,而且执行起来可能稍有不同。我反汇编了这两个变量,并为apply方法提供了一个额外的属性查找,这可能解释了这一差异:>>>timeit.timeit(“e=np.exp(x)”,setup=”导入熊猫作为pd;导入numpy作为np;x=pd.Series(np.linspace(-10,10,2001)))110.47226533893357>>>timeit.timeit(“e=x.apply(np.exp)”,setup=“导入熊猫作为pd;导入numpy作为np;x=pd.Series(np.linspace(-10,10,2001))”11162233564382561@Confounded用原始数据、代码重新生成df、尝试、完整错误跟踪和所需结果发布新问题。在注释中回答问题会适得其反我尝试过,但它返回“TypeError:ufunc的循环不支持没有可调用exp方法的float类型的参数0”