-2

I have a data frame with 5 columns, I only want to add the second and the third, but each point in the third column has to be multiplied by 3,

so I need to add a new column called
"Total score" which is df['Second'] + 3* df['Third']

I have tried with sum but I don't know how to indicate that I want weigh and select only two columns

tawab_shakeel
  • 3,701
  • 10
  • 26
Nelly Louis
  • 157
  • 2
  • 8

1 Answers1

1

Make sure your columns is order correctly, then we can using dot

df['Total Score'] = df.dot([0,1,3,0,0]) 

Or to be safe

df['Total Score'] = df[['Second','Third']].dot([1,3]) 
BENY
  • 317,841
  • 20
  • 164
  • 234
  • I always get this error "A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead" – Nelly Louis Jun 29 '19 at 20:42
  • @NellyLouis that is set copy error , so you may want to check with https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas – BENY Jun 29 '19 at 20:43