I'm trying to create new columns in a pandas df based on values from existing columns. Does anyone know a good way of doing that?
Basically I need to get the value from one column and create new colum using the value from another existing column as a name
df['a'], df['b'], df['c'], df['d'] - - - so if the value in df['a'] is D then I need to create a new column name from value df['a'] and insert the values from df['d'] in each row that matches this criteria
not sure if I was clear enough but I would appreciate any help. the total columns would be more than 30, so I would like to know a smart way of doing that instead of manually typing df['new column'] = df.apply .......
def view_conv(activity, activityId, total_conv, view_conv, click_conv):
if activityId == '10649748':
return view_conv
def click_conv(activity, activityId, total_conv, view_conv, click_conv):
if activityId == '10649748':
return click_conv
d['Insights Programmes - STEM Internships Application Start - View-through Conversions'] = d[['Activity','Activity ID', 'Total Conversions', 'View-through Conversions', 'Click-through Conversions']].apply(lambda x: view_conv(*x), axis=1)
d['Insights Programmes - STEM Internships Application Start - Click-through Conversions'] = d[['Activity','Activity ID', 'Total Conversions', 'View-through Conversions', 'Click-through Conversions']].apply(lambda x: click_conv(*x), axis=1)
I'm sure this is not the best way of doing this, there will be a few different values in column activity so I need to know a smarter way of doing this