pythonpandas

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:

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