mercredi 15 avril 2020

if elif else AttributeError: ("'str' object has no attribute 'str'", 'occurred at index 0')

I'm trying to create a new column with replaced strings according to the conditions below. I keep getting error saying: AttributeError: ("'str' object has no attribute 'str'", 'occurred at index 0') Can anyone help me with the error? Much appreciated!

wage = ['daily', 'hour','month','piece','daily&hour','daily&month','daily&piece','hour&month','hour&piece','month&piece','none']

dict = {'Wage_System': wage}

q2 = pd.DataFrame(dict) 

def get_output(q2):
    if q2['Wage_System'].str.contains('hour'):
        return 'Hourly'
    elif q2['Wage_System'].str.contains('daily'):
        return 'Daily'
    elif q2['Wage_System'].str.contains('month'):
        return 'Monthly'
    elif q2['Wage_System'].str.contains('piece'):
        return 'Piece'

    elif q2['Wage_System'].str.contains('hour') and q2['Wage_System'].str.contains('daily'):
        return 'Mixed'
    elif q2['Wage_System'].str.contains('hour') and q2['Wage_System'].str.contains('monthly'):
        return 'Mixed'
    elif q2['Wage_System'].str.contains('hour') and q2['Wage_System'].str.contains('piece'):
        return 'Mixed'    

    elif q2['Wage_System'].str.contains('daily') and q2['Wage_System'].str.contains('monthly'):
        return 'Mixed'
    elif q2['Wage_System'].str.contains('daily') and q2['Wage_System'].str.contains('piece'):
        return 'Mixed'    

    elif q2['Wage_System'].str.contains('monthly') and q2['Wage_System'].str.contains('piece'):
        return 'Mixed'  

    else:
        return 'Not Specified'

q2['New Wage System'] = q2.apply(get_output, axis=1)

Aucun commentaire:

Enregistrer un commentaire