创建新的计算字段

2024-04-19 17:54:41 发布

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

如何在熊猫中创建另一个字段的日志?你知道吗

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

Tags: csvtxtlogtruedfreadindexparse
1条回答
网友
1楼 · 发布于 2024-04-19 17:54:41

Pandas有很多内置的功能,可以在序列和数据帧上以矢量化的方式应用函数。如果失败,可以使用mapapply。这里map将对一个系列应用一个函数元素。你知道吗

df['exr_ln'] = df['exr'].map(math.log)

有关map和apply的更多信息,请参见this answer。你知道吗

相关问题 更多 >