Skip to content Skip to sidebar Skip to footer

Python Function Error : '<' Not Supported Between Types 'str' And 'int'

I am working on repeats classification project. I am calculating time lapse in between a repeat and fresh mail in days. I want to apply a function on this time lapse which states w

Solution 1:

That basically means that your 'days ' variable is a string. You can not compare strings with integers using "<". Try :

...
if(int(days)<30):
...

Solution 2:

One recommendation which you should consider, always perform a search with the exact error you get from python, and often times you get your response within 0.1 ms.

In your case, you are comparing an integer value (let's say 2) to a string value, which happens to be '2') and python does not understand.

You can compare `int(days)' and 30 like:

if(int(day) < 30):
    return'repeat'else: 
    return'fresh'

Solution 3:

The error is self-explaining :

Python function error : '<' not supported between types 'str' and 'int'

This is caused by a comparison of str and int types, which is invalid in Python 3 (although it's ok in Python 2).

Example :

result = '20' > 10# In Python 3, this is illegal and will raise an exception

In your case, the error is most probably caused by the test if(days<30):. Your dataframe probably contains str values. You may need to convert them to int before trying to compare to another value:

days = int(df['days_difference'])
if(days<30):
   return'repeat'else:
   return'fresh'

Post a Comment for "Python Function Error : '<' Not Supported Between Types 'str' And 'int'"