0

I have a dataframe, df and lets say it has two columns, Score & Gender.

I want to create a vector based on the gender, for example if a row has a gender value of 'male' the I want the value to be 'b' and if the value is 'female' I want the value to be 'r'

I know I can loop through the rows of a dataframe however I read that its better to use vectorisation. So without looping through the dataframe how would I obtain the vector (Result I want)?

 Score    Gender       Result I want
 34       male         b
 22       male         b
 29       female       r
 25       male         b
 36       female       r 
mHelpMe
  • 6,336
  • 24
  • 75
  • 150
  • 1
    `df['Result I want']=np.select([df['Gender'].eq('male'),df['Gender'].eq('female')],['b','r'])` – anky Jul 13 '19 at 15:27
  • @anky_91 thanks for the comment. Also I think you are correct about this being a duplicate question, thanks for the link. Shall I delete this post? – mHelpMe Jul 13 '19 at 15:28
  • Umm, your wish actually :) not mandatory – anky Jul 13 '19 at 15:34
  • 1
    I will leave it I think, if someone else comes across this they can follow the link you posted. Cheers again – mHelpMe Jul 13 '19 at 15:53

0 Answers0