I want to create an additional feature(column) based on some manipulation of values from existing features

datascience.stackexchange https://datascience.stackexchange.com/questions/40135

Question

Consider my data-frame to be like this ('x','y','z' are features):

enter image description here

I want to create a python function which will take an expression as a string (something like this: 'x+y-2z') and create a new feature by evaluating the expression. Output should be like:

enter image description here

I want to generalize this function so that it will work for different data-frames with different column(feature) names in the expression.

Edit- I have a prototype of the desired function(named 'parser'):

def parser(exp):

df['new_col'] = df.apply(lambda row: row.x+row.y-2*row.z, axis=1)

However, I want to generalize this part - row.x+row.y-2*row.z so that it will adjust itself according to the string(i.e. expression) provided as its argument.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with datascience.stackexchange
scroll top