Home > Blockchain >  Meaning of "y = df['something'].apply(lambda x: 1 if x== 'yes' else 0)"
Meaning of "y = df['something'].apply(lambda x: 1 if x== 'yes' else 0)"

Time:08-12

This Syntax is the second line after uploading a csv file by pandas library and get_dummies drop, i want to understand this syntax better in order to make use of it thank !

y = df['something'].apply(lambda x: 1 if x== 'yes' else 0)

CodePudding user response:

Your code means:

 y = df['something'].apply(lambda x: 1 if x== 'yes' else 0)

Test values in column something and return 1 if match yes else return 0 in new Series in variable y.

Btw, vectorized solution is:

 #return Series
 y = (df['something'] == 'yes').astype(int)

 #return 1d array
 y = np.where(df['something']== 'yes', 1, 0)
  • Related