Can't convert to Float error given when using DataFrame and math.exp Error

Viewed 55

I am trying to get part of a larger piece of code to work so have pulled out the problem element and created a mini code for testing.

import math
import csv
import pandas as pd
from pandas import DataFrame
print(df)    

data= pd.read_csv('miniDF.csv')
df=pd.DataFrame(data, columns=['x'])
df['y']=(12.775*math.exp(-1.494*df['x']))

print(df)

The x column in the df are 0.01,0.1,0.5,1.5,2.9 Just simple float values that mimic my real DataFrame. If I give the equation a single 'x' value in the code, the maths works correctly, but it doesn't work when pulling x values from the DataFrame. The shell output and error I get is:

     X
  0  0.01
  1  0.05
  2  0.10
  3  0.15
  4  1.00
  5  2.90
  Traceback (most recent call last):
  File "/Users/willhutchins/Desktop/minitest.py", line 11, in <module>
  df['y']=(12.775*math.exp(-1.494*df['X']))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/series.py", line 131, in wrapper
raise TypeError("cannot convert the series to " "{0}".format(str(converter)))
TypeError: cannot convert the series to <class 'float'>

Ultimately, I want to use it like this:

df['SBTn']=np.where(df['Fr']<=(12.775*math.exp(-1.494*(df['Fr']))),1,df['SBTn'])

Assuming the first question can be answered, does anybody foresee any problems using it in the np.where version?

1 Answers

Use np.exp method for working with arrays, math.exp working with scalars:

import numpy as np

df['y']=(12.775*np.exp(-1.494*df['x']))
print (df)
      x          y
0  0.01  12.585560
1  0.05  11.855479
2  0.10  11.002144
3  0.15  10.210230
4  1.00   2.867642
5  2.90   0.167779

You can loop in apply for math.exp, but it is slowier:

df['y']=(12.775*df['x'].apply(lambda x: math.exp(-1.494*x)))
print (df)
      x          y
0  0.01  12.585560
1  0.05  11.855479
2  0.10  11.002144
3  0.15  10.210230
4  1.00   2.867642
5  2.90   0.167779

#6k rows
df = pd.concat([df] * 1000, ignore_index=True)

In [14]: %timeit df['y1']=(12.775*np.exp(-1.494*df['x']))
658 µs ± 19.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [15]: %timeit df['y2']=(12.775*df['x'].apply(lambda x: math.exp(-1.494*x)))
3.2 ms ± 133 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Related