1

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!

2
  • Try df['A'] = pd.to_numeric(df['A'], errors='coerce') before comparison? Commented Dec 10, 2020 at 4:48
  • This one worked thank you Quang Hoang Commented Dec 10, 2020 at 5:20

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

2 Comments

this one returned an error "invalid literal for int() with base 10..." but thanks anyway Alex Watt
This is the correct answer. Please show the full message error you got @alexr. This is probably a ValueError, then it should show the first value that break the conversion. Also, try to use float instead of int.
0

Are you working with Google Colab? If thats the case, then do both: restart runtime and restart everything. It usually works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.