Mapping each value to one of the career stage definitions, function help

Hello Dataquest community!

Can someone help me understand why I am getting an error if I start my function code to search for NaN values first? or is there is something else that I am unable to identify? Thanks!

Screen Link:

My Code:

def career_stage(val):
    if pd.isnull(val):
        return np.nan
    elif val < 3:
        return "New"
    elif 3 <= val <7:
        return "Experienced"
    elif 7 <= val <11:
        return "Established"
        return "Veteran"

combined_updated['service_cat'] = combined_updated['institute_service'].apply(career_stage)

What I expected to happen:

What actually happened:

TypeErrorTraceback (most recent call last)
<ipython-input-36-7d18d0064857> in <module>()
     11         return "Veteran"
---> 13 combined_updated['service_cat'] = combined_updated['institute_service'].apply(career_stage)

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ in apply(self, func, convert_dtype, args, **kwds)
   2549             else:
   2550                 values = self.asobject
-> 2551                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   2553         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()

<ipython-input-36-7d18d0064857> in career_stage(val)
      2     if pd.isnull(val):
      3         return np.nan
----> 4     elif val < 3:
      5         return "New"
      6     elif 3 <= val <7:

TypeError: unorderable types: str() < int()

Simply, python was expecting an integer to compare to the values in the if statements but it found a string instead! :slight_smile:

and it says so here:

TypeError: unorderable types: str() < int()

you can fix this by converting val to an integer first before the if statements: int(val)

Error messages (Traceback) mostly point out the origin of the error. For example, the Traceback you get, points to -

elif val < 3:

And then it states the reason for the error -

TypeError: unorderable types: str() < int()

It basically says that there was a TypeError because your code is trying to compare a string datatype with an integer datatype.

So, that explains you the source and cause of the error. I would recommend getting comfortable with reading error messages and also googling for these messages (like the TypeError statement) to be able to debug the problem.

Now, coming to why you get this error.

It’s because you are using apply() on combined_updated['institute_service'].

Your institute_service column has string values in it. You need to make sure that you follow the instructions so that the column has the correct datatype for its values.

Thank you guys for helping me figure this out!

I had already extracted the years and may data type was integer. Figured out I wasn’t using the correct name series.apply(). It was institute_service_updated instead of institute_service, hence the error.