如何在熊猫中创建另一个字段的日志?你知道吗
df =pd.read_csv('tseries.txt',parse_dates=True,index_col=0)
df['exr_ln']=math.log(df['exr'])
Traceback (most recent call last):
File "/home/ubuntu/workspace/chaos/forecast.py", line 212, in <module>
df['exr_ln']=math.log(df['exr'])
TypeError: only length-1 arrays can be converted to Python scalars
Pandas有很多内置的功能,可以在序列和数据帧上以矢量化的方式应用函数。如果失败,可以使用
map
或apply
。这里map
将对一个系列应用一个函数元素。你知道吗df['exr_ln'] = df['exr'].map(math.log)
有关map和apply的更多信息,请参见this answer。你知道吗
相关问题 更多 >
编程相关推荐