-1

How do I create a new field in pandas that is a log of another field?

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
Tampa
  • 75,446
  • 119
  • 278
  • 425

1 Answers1

1

Pandas has a lot of built-in functionality to apply functions in a vectorized way over Series and DataFrames. When that fails, you can use map or apply. Here map will applies a function element-wise on a Series.

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

For more on map and apply, see this answer.

Community
  • 1
  • 1
exp1orer
  • 11,481
  • 7
  • 38
  • 51