Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a dataframe from another dataframe and a list of formulas in Pandas Dataframe

I have the following list and dataframe:

import pandas as pd

df = pd.DataFrame({
    'name': ['alice','bob','charlie'],
    'a0': [25,26,27],
    'b0': [10,11,12],
    'c0': [3,4,5],
})

formul=['a0+b0','a0-c0','b0*c0','a0+c0']

from this i want to build a new dataframe in which the first column is the original and the others are modified according to the operations in the list:

name    a0+b0 a0-c0 b0*c0 a0+c0
alice      35    22    30    28
bob        37    22    44    30
charlie    39    22    60    32

I have developed the formula in R, but now i want to translate it to python:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Formula<-strsplit(formul, split=",")[[1]]
df<-as.data.frame(cbind(as.numeric(df$Name),sapply(Formula, function(x) with(df, eval(parse(text = x))))))

regards

>Solution :

You could combine assign and eval:

out = df.assign(**{s: lambda x: x.eval(s) for s in formul})

Output:

      name  a0  b0  c0  a0+b0  a0-c0  b0*c0  a0+c0
0    alice  25  10   3     28     28     28     28
1      bob  26  11   4     30     30     30     30
2  charlie  27  12   5     32     32     32     32

Or, for a new DataFrame:

tmp = df.set_index('name')
out = pd.DataFrame({s: tmp.eval(s) for s in formul})

# or

out = (pd.DataFrame({s: df.eval(s) for s in formul})
         .set_axis(df['name'])
      )

Output:

         a0+b0  a0-c0  b0*c0  a0+c0
name                               
alice       35     22     30     28
bob         37     22     44     30
charlie     39     22     60     32
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading