Vikrant Arora
Vikrant Arora

Reputation: 198

Despite being a numeric column getting error "'>' not supported between instances of 'str' and 'int'"

From a dataframe 'movie1' I need to create a new one 'movie2007to16' which has data from year 2007 & beyond. The column name is 'title_year', which was originally float type but had some NaN values which I replaced with 0's. I then converted it into an int8 type.

But when I try to create the new dataframe with 'title_year'>2006 i get

"TypeError: '>' not supported between instances of 'str' and 'int'".

What am I doing wrong?

Went through answers on this exact error on stackoverflow and other sites and tried the solutions but no luck.

movie1['title_year']=pd.to_numeric(movie1['title_year'], errors='coerce').fillna(0).astype(np.int8)

movie2007to16=movie1[movie1['title_year'>2006]]

I expect a new dataframe which has records for 2007 & beyond.

Upvotes: 0

Views: 55

Answers (1)

wpercy
wpercy

Reputation: 10090

You're comparing the string literal 'title_year' to the integer 2006 here:

'title_year'>2006

Try:

movie2007to16 = movie1[movie1['title_year'] > 2006]

Upvotes: 2

Related Questions