Python 熊猫-创建新的计算字段

Python 熊猫-创建新的计算字段,python,pandas,Python,Pandas,如何在熊猫中创建一个新的字段,它是另一个字段的日志 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']

如何在熊猫中创建一个新的字段,它是另一个字段的日志

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
df=pd.read\u csv('tseries.txt',parse\u dates=True,index\u col=0)
df['exr_ln']=math.log(df['exr'])
回溯(最近一次呼叫最后一次):
文件“/home/ubuntu/workspace/chaos/forecast.py”,第212行,在
df['exr_ln']=math.log(df['exr'])
TypeError:只有长度为1的数组才能转换为Python标量

熊猫有很多内置功能,可以通过矢量化的方式在系列和数据帧上应用函数。如果失败,您可以使用
map
apply
。此处
map
将在序列上应用函数元素

df['exr\u ln']=df['exr'].map(math.log)

有关映射和应用的详细信息,请参见