Problem solving: error in reading CSV file by Panda: typeerror: invalid type comparison

When reading and processing the data in the CSV file with panda in Python, you may encounter such an error:

TypeError: invalid type comparison

Invalid type comparison

At this time, you can print the data in your dataframe

1. There may be no data in some items, which will be displayed as Nan when printing. Nan can’t be compared with any data, and it’s not equal to any value, including himself (so you can also use a= A to judge whether a is Nan

Therefore, in the following data processing, if a comparison operation is performed, an error will be reported:

Typeerror: invalid type comparison
the solution is to add parameters when reading the CSV

keep_ default_ Na = false
in this way, entries without data will be recognized as empty characters instead of Nan

2. Maybe the data types of different columns in your dataframe are different. Some of them are recognized as STR and some as int. although they all look like numbers, they will also report errors when compared later

At this time, you can add a parameter

Converters = {from ‘: STR,’ to ‘: STR} # convert both from column and to column to STR type

converters are interpreted as follows:

converters : dict, default None
Dict of functions for converting values in certain columns. Keys can either
be integers or column labels

After the same type, they can be compared together

Similar Posts: