0

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 .......

DF with original columns

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)

Expected output

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

eblucas
  • 1
  • 3
  • Does this answer your question? [Combine two columns of text in pandas dataframe](https://stackoverflow.com/questions/19377969/combine-two-columns-of-text-in-pandas-dataframe) – sophros Jan 20 '21 at 12:19
  • @sophros thanks for this. To be honest it doesn't. There are a lot of different values in column df['a'] which I will use to create new columns , around 30 new columns using values from df['a'] as name and values from other columns based on some condition. – eblucas Jan 20 '21 at 12:32
  • Kindly share sample data with expected output – sammywemmy Jan 20 '21 at 12:37
  • Any ideas guys? I appreciate any help – eblucas Jan 22 '21 at 18:44

0 Answers0