jeudi 14 janvier 2021

Encoding column pandas using if condition

I am trying to encode a weekday_name column (str) in my dataframe to int. But I am getting the error The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Could someone please help me understand why this error comes and what would be solution

Code I am trying with is:

def weekday_df(df):

    if (data_semi['Weekday_Name'] == 'Monday'):
        return 1
    elif (data_semi['Weekday_Name'] == 'Tuesday'):
        return 2
    elif (data_semi['Weekday_Name'] == 'Wednesday'):
        return 3
    elif (data_semi['Weekday_Name'] == 'Thursday'):
        return 4
    elif (data_semi['Weekday_Name'] == 'Friday'):
        return 5
    elif (data_semi['Weekday_Name'] == 'Saturday'):
        return 6
    elif (data_semi['Weekday_Name'] == 'Sunday'):
        return 7
    

data_semi['Weekday_Name'] = data_semi.apply(weekday_df, axis = 1)

Dataframe looke like below enter image description here

Aucun commentaire:

Enregistrer un commentaire