Guided Project: Clean And Analyze Employee Exit Surveys - Part 9 - None

Screen Link:

https://app.dataquest.io/m/348/guided-project%3A-clean-and-analyze-employee-exit-surveys/9/clean-the-service-column

My Code:

combined_updated['institute_service_up'] = combined_updated['institute_service'].astype('str').str.extract(r'(\d+)')
combined_updated['institute_service_up'].astype('float')

def category(x):
    if pd.isnull(x):
        return np.NaN
    elif int(x) < 3:
        return 'New'
    elif 3 < int(x) < 7:
        return 'Experienced'
    elif 7 < int(x) < 11:
        return 'Established'
    elif int(x) > 11:
        return 'Veteran'
 

combined_updated['service_cat'] = combined_updated['institute_service_up'].apply(category)

combined_updated['service_cat'].head(10)```

What I expected to happen:
list of values defined in the ‘category’ function.

What actually happened:

0           None
1        Veteran
2           None
3        Veteran
4           None
5        Veteran
6    Experienced
7            NaN
8        Veteran
9        Veteran
Name: service_cat, dtype: object```

Good day,

I noticed that “None” values appear after running the function. I am struggling to understand where this “None” came from because there should only be five values i.e. NaN, New, Established, Experienced, Veteran.

My guess here is that this happens because the function you wrote does not return anything when the value is 3, 7 and 11. For example, in the code below, values less than 3 return “New”, values greater the 3 return “Experienced” but values equal to 3 return nothing.

elif int(x) < 3:
    return 'New'
elif 3 < int(x) < 7:
    return 'Experienced'

To fix this, you should use <= instead of only <, your code would then look like this:

def category(x):
    if pd.isnull(x):
        return np.NaN
    elif int(x) < 3:
        return 'New'
    elif 3 <= int(x) < 7:
        return 'Experienced'
    elif 7 <= int(x) < 11:
        return 'Established'
    elif int(x) >= 11:
        return 'Veteran'

I hope this helps you.

Yes that was exactly the problem. Thank you!

1 Like