How To Create New Columns As A Function Of Existing Columns In A Pandas Dataframe?
To start with, I have a non-linear model, that I would like to perform a lasso regression with: My approach to doing this would be to create a new data frame containing all possib
Solution 1:
If I understand your requirements correctly then maybe this will work for you. If not then maybe you can use the concepts to meet your needs.
Create a list with your numeric predictor values: (I created some representative via range()
)
p19 = range(10,29)
Generate all the combinations from that list:
combs = list(itertools.combinations(p19, 2))
Perform the operation you require on each combination of numbers: (I simply multiply them together)
col_vals = [x[0]*x[1] for x in combs]
Using the predictor list, create a DF with some column headings:
df_p = pd.DataFrame(p19).transpose().rename(columns = lambda x: 'P' + str(x+1))
Using the computed column values, create a values DF with some column headings:
df_c = pd.DataFrame(col_vals).transpose().rename(columns = lambda x: 'C' + str(x+1))
Then combine the two DFs:
df = pd.concat([df_p,df_c], axis=1)
Post a Comment for "How To Create New Columns As A Function Of Existing Columns In A Pandas Dataframe?"