Home > other >  How to multiply a column in panda frame with a function
How to multiply a column in panda frame with a function

Time:07-31

I have a data text file with two columns (called X and Y):

-2.321404951 0.6231752828E-20
-2.311404951 0.2386233219E-19
-2.301404951 0.8958714819E-19
-2.291404951 0.3297711248E-18
-2.281404951 0.1190194012E-17
-2.271404951 0.4211777921E-17
-2.261404951 0.1461370397E-16
-2.251404951 0.4971725004E-16
-2.241404951 0.1658482566E-15
-2.231404951 0.5424716898E-15
-2.221404951 0.1739847682E-14
-2.211404951 0.5471662415E-14
-2.201404951 0.1687359166E-13
-2.191404951 0.5102494608E-13
-2.181404951 0.1513043201E-12
-2.171404951 0.4399681358E-12
-2.161404951 0.1254584823E-11
-2.151404951 0.3508293965E-11
-2.141404951 0.9620945929E-11
-2.131404951 0.2587465918E-10
-2.121404951 0.6824586233E-10
-2.111404951 0.1765359553E-09
-2.101404951 0.4478742859E-09

I would like to obtain data by the formula f(X)*Y with f(X) = 1/(exp(X)-5). Here is my code, but it does not work:

import pandas as pd
import numpy as np
import sys
df = pd.read_csv("dos.txt", sep='delimiter', header=None, engine='python')
F = 1 /  (np.exp((df.loc[:,0] - 5)))
y=df.loc[:,1]*F
print(y)

The error is TypeError: cannot convert the series to <class 'float'>. Can anyone help me out?

CodePudding user response:

You didn't provide a proper separator for parsing the CSV file, so your data was parsed as one string per line instead of multiple numerical columns. Here's a fix:

import pandas as pd
import numpy as np
import sys
df = pd.read_csv("dos.txt", sep="\s ", header=None, engine='python')
F = 1 /  (np.exp((df.loc[:,0] - 5)))
y=df.loc[:,1]*F
print(y)

CodePudding user response:

Another way:

def expnFn(x):
    return 1/(np.exp(x)-5)

df['Z'] = df.apply(lambda x: x['X']*expnFn(x['Y']), axis = 1)



 X             Y         Z
0  -2.321405  6.231753e-21  0.580351
1  -2.311405  2.386233e-20  0.577851
2  -2.301405  8.958715e-20  0.575351
3  -2.291405  3.297711e-19  0.572851
4  -2.281405  1.190194e-18  0.570351
5  -2.271405  4.211778e-18  0.567851
6  -2.261405  1.461370e-17  0.565351
7  -2.251405  4.971725e-17  0.562851
8  -2.241405  1.658483e-16  0.560351
9  -2.231405  5.424717e-16  0.557851
10 -2.221405  1.739848e-15  0.555351
11 -2.211405  5.471662e-15  0.552851
12 -2.201405  1.687359e-14  0.550351
13 -2.191405  5.102495e-14  0.547851
14 -2.181405  1.513043e-13  0.545351
15 -2.171405  4.399681e-13  0.542851
16 -2.161405  1.254585e-12  0.540351
17 -2.151405  3.508294e-12  0.537851
18 -2.141405  9.620946e-12  0.535351
19 -2.131405  2.587466e-11  0.532851
20 -2.121405  6.824586e-11  0.530351
21 -2.111405  1.765360e-10  0.527851
22 -2.101405  4.478743e-10  0.525351

CodePudding user response:

To resolve your float issue, you need to define the dtype when calling read_csv. dtype takes a dict something like:

{‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’}

see: https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html.

Then you can just apply a formula to the columns directly, something like:

import pandas as pd, numpy as np

df = pd.DataFrame({ 'x': [1, 2, 3, 4, 5], 'y': [6, 7, 8, 9, 10]})
df['z'] = (1 / (np.exp(df.x) - 5)) * df.y

or if you want to use a function:

def fn(x, y):
     return (1 / (np.exp(x) - 5)) * y
df['z'] = fn(df.x, df.y)
  • Related