Home > Net >  '<' not supported between instances of 'str' and 'int' in Python
'<' not supported between instances of 'str' and 'int' in Python

Time:11-21

When I try to create a new variable in dataframe Call08q1_09q1 by adding two float variable

Call08q1_09q1['MBS']=Call08q1_09q1['RCFD8639'] Call08q1_09q1['RCFD2170']

the error below shows up:

'<' not supported between instances of 'str' and 'int' in Python

However, I don't have string in my dataframe.

Call08q1_09q1.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 39675 entries, 0 to 39674
Data columns (total 20 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   RSSD9001  39675 non-null  float64
 1   RSSD9999  39675 non-null  float64
 2   RCFD2170  39673 non-null  float64
 3   RCFD8639  38166 non-null  float64
 4   RCFD8641  38166 non-null  float64
 5   RCFD8639  38166 non-null  float64
 6   RCFD0211  38166 non-null  float64
 7   RCFD1287  38166 non-null  float64
 8   RCON3531  1107 non-null   float64
 9   RCFD1289  38166 non-null  float64
 10  RCFD1294  38166 non-null  float64
 11  RCFD1293  38166 non-null  float64
 12  RCFD1298  38166 non-null  float64
 13  RCON3532  1111 non-null   float64
 14  RCFD3210  38443 non-null  float64
 15  RIAD4230  38398 non-null  float64
 16  RIAD4340  38441 non-null  float64
 17  RCFD2122  39644 non-null  float64
 18  RCFD2125  249 non-null    float64
 19  RCFD1600  52 non-null     float64
dtypes: float64(20)

CodePudding user response:

You have loads of nulls in your columns as the printout tells you. How are those represented? Can you add these nulls with ints? I suggest you debug by inspecting these null values and taking appropriate action to fill them, drop them, or otherwise transform them into something useful.

CodePudding user response:

The error has not occured in the line of code below since this one does not contain any comparison operator (<, >, ..).

Call08q1_09q1['MBS']= Call08q1_09q1['RCFD8639']   Call08q1_09q1['RCFD2170']

The error has for sure occured in a line where you try to compare a string with a number (int) like the scenario below :

s="1"
n= 3
s < n
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [1], line 3
      1 s="1"
      2 n= 3
----> 3 s<n

TypeError: '<' not supported between instances of 'str' and 'int'

To fix that you need to cast the string as a number :

int(s) < n
#True
  • Related