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 data frame
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
Method, add parameters when reading the CSV
keep_default_na=False
In this way, entries without data will be recognized as null 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 the from and to columns to str
The explanation of converters is as follows:
converters: dict, default None
Dict of functions for converting values in certain columns. Keys can either
be integers or column labels
The same type can be compared together
Read More:
- [Solved] YOLOv5 Model training error: TypeError: new(): invalid data type ‘str’
- Python TypeError: Unrecognized value type: <class ‘str‘>dateutil.parser._parser.ParserError: Unknow
- Python scatter chart error: TypeError: object of type ‘NoneType’ has no len()
- Python: How to Reshape the data in Pandas DataFrame
- Python Pandas Error: KeyError: 0 [How to Solve]
- Python+ Pandas + Evaluation of Music Equipment over the years (Notes)
- [Solved] TypeError: Object of type ‘bytes’ is not JSON serializable
- [Solved] Python Pandas Read Error: OSError: initializing from file failed
- Pandas read_csv pandas.errors.ParserError: Error tokenizing data
- Python Pandas Error: No module named ‘openpyxl‘
- [Solved] TypeError: unsupported operand type(s) for /: ‘str‘ and ‘int‘
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- How to Solve Python Pandas Read or Import Files Error
- Error reading file by pandas pandas.errors.EmptyDataError: no columns to parse from file
- TypeError: Decimal type object is not a JSON serialization solution
- Typeerror in Python regular expression: expected string or bytes like object
- Python opens the table and appears pandas.errors.ParserError: Error tokenizing data. C error:
- Python TypeError: not all arguments converted during string formatting [Solved]
- Python 3.X error: valueerror: data type must provide an itemsize