Reputation: 11
I am comparing two columns in a pandas DF, A and B like this:
df['A'].gt(df['B'])
but get this error:
TypeError: '>' not supported between instances of 'str' and 'int'
when I lookup the dtypes: df.dtypes
I see this output:
id: object
A: object
B: int64
when I look at 5 top records with do df.head()
I see this:
id A B
a1 2 2.353566677998
a2 4 4.454444231211
a3 3 6.777888665343
.....
how to solve this error:
TypeError: '>' not supported between instances of 'str' and 'int'
when doing this comparison:
df['A'].gt(df['B'])
Looks like many people are asking this but I couldn't find one that matches my issue!
Upvotes: 1
Views: 2142
Reputation: 56
Are you working with Google Colab? If thats the case, then do both: restart runtime and restart everything. It usually works.
Upvotes: 0
Reputation: 937
Although the values are clearly integers when you look at the DataFrame, pandas uses object
for str
values - which from the error, is what you have. You can cast all of the values of the A
column to integers though, and you should be good to go!
df["A"] = df["A"].astype(int)
Upvotes: 2