mercredi 11 mars 2020

Creating dataframe column with conditional logic (when get dummies won't work)

I have a dataframe with a Day of the Week column. I have used get dummies for feature selection - however the sample of data I am dealing with only has 3 days of the week listed. However for the model I've built I need to consider all 7 days of the week.

Is there a better way of doing it that have 7 lines of code using np.where like the below?

    df['isMonday'] = np.where(df['date_DoW']=='Monday', 1, 0 )
    df['isTuesday'] = np.where(df['date_DoW']=='Tuesday',  1, 0 )
    df['isWednesday'] = np.where(df['date_DoW']=='Wednesday',  1, 0 )
    df['isThursday'] = np.where(df['date_DoW']=='Thursday',  1, 0 )
    df['isFriday'] = np.where(df['date_DoW']=='Friday',  1, 0 )
    df['isSaturday'] = np.where(df['date_DoW']=='Saturday',  1, 0 )
    df['isSunday'] = np.where(df['date_DoW']=='Sunday',  1, 0 )

Aucun commentaire:

Enregistrer un commentaire