Sergio Espejo Sergio Espejo - 3 months ago 14
Python Question

How do you use the values of a dataframe as a variable in a function to make a different column

I have a dataframe which i want to use as the values in it as the "x" in the function

0.00459652*np.exp(4.5*x)+0.000984312

here is my code

df=pd.read_csv('F:/Data32.csv')
df2=df['Temperature']
x=df2.values.tolist()
test=df2.apply(0.00459652*np.exp(4.5*x)+0.000984312)
test


I keep getting the error
TypeError: can't multiply sequence by non-int of type 'float'

Answer

If you want to apply the function 0.00459652*np.exp(4.5*x)+0.000984312 to each value in the Temperature column, just put that column at the x location:

test = 0.00459652*np.exp(4.5*df['Temperature'])+0.000984312

The reason why you get that error is that x is a python list, which cannot be multiplied by a floating number (4.5 in this case). But df['Temperature'] can (it's a pandas.Series)

Also, 0.00459652*np.exp(4.5*x)+0.000984312 is not a python function. You need lambda x : 0.00459652*np.exp(4.5*x)+0.000984312

Comments