Home > Net >  multiplying column of the file by exponential function
multiplying column of the file by exponential function

Time:11-26

I,m struggling with multiplying one column file by an exponential function so my equation is y=10.43^(-x/3.0678) 0.654 The first values in the column are my X in the equation, so far I was able to multiply only by scalars but with exponential functions not

the file looks like this

8.09
5.7
5.1713
4.74
4.41
4.14
3.29
3.16
2.85
2.52
2.25
2.027
1.7
1.509
0.76
0.3
0.1 

So after the calculations, my Y should get these values

8.7     0.655294908
8.09    0.656064021
5.7     0.6668238549
5.1713  0.6732091509
4.74    0.6807096436
4.41    0.6883719253
4.14    0.6962497391
3.29    0.734902438
3.16    0.7433536016
2.85    0.7672424605
2.52    0.7997286905
2.25    0.8331287249
2.027   0.8664148415
1.7    0.926724933
1.509   0.9695896976
0.76    1.213417197
0.3    1.449100509
0.1    1.580418766````

So far this code is working for me but it´s far away from what i want

from scipy.optimize import minimize_scalar 
import math 
col_list = ["Position"]
df = pd.read_csv("force.dat", usecols=col_list)
print(df)
A = df["Position"] 
X = ((-A/3.0678 0.0.654)
print(X)

CodePudding user response:

If I understand it correctly you just want to apply a function to a column in a pandas dataframe, right? If so, you can define the function:

def foo(x):
    y = 10.43 ** (-x/3.0678) 0.654
    return y

and apply it to df in a new column. If A is the column with the x values, then y will be

df['y'] = df.apply(foo,axis=1)

Now print(df) should give you the example result in your question.

CodePudding user response:

You can do it in one line:

>>> df['y'] = 10.43 ** (- df['x']/3.0678) 0.654
>>> print(df)

         x         y
0   8.0900  0.656064
1   5.7000  0.666824
2   5.1713  0.673209
3   4.7400  0.680710
4   4.4100  0.688372
5   4.1400  0.696250
6   3.2900  0.734902
7   3.1600  0.743354
8   2.8500  0.767242
9   2.5200  0.799729
10  2.2500  0.833129
11  2.0270  0.866415
12  1.7000  0.926725
13  1.5090  0.969590
14  0.7600  1.213417
15  0.3000  1.449101
16  0.1000  1.580419
  • Related