def new_calculate(x, formulas):
return pd.Series((eval(formula)) for formula in formulas)
col = {
's1':{'l':100, 'w':200},
's2':{'l':200, 'w':400},
's3':{'l':300, 'w':500}
}
coldf = pd.DataFrame.from_dict(col, orient='index')
cols = ['new_a', 'new_p']
formulas = ["x['l']+x['w']", "x['l']+x['w']+x['l']+x['w']"]
coldf[cols] = coldf.apply(lambda x : new_calculate(x, formulas), axis=1)
I am getting an error saying NameError: name 'x' is not defined
I am trying to produce a resulting dataframe with additional columns.
l w new_a new_p
s1 100 200 20000 600
s2 200 400 80000 1200
s3 300 500 150000 1600
What is wrong? Can itertuples be used or any other way to do it in an efficient way?
I am trying to follow these examples Add Multiple Columns to Pandas Dataframe from Function, Merge dataframe with another dataframe created from apply function?