Python error "TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'"

0 votes

I'm trying to execute the following python code:

import pandas as pd                                                                        

csv_file = 'sample.csv'                                                                    
count = 1                                                                                  
my_filtered_csv = pd.read_csv(csv_file, usecols=['subDirectory_filePath', 'expression'])   

emotion_map = { '0':'6', '1':'3', '2':'4', '3':'5', '4':'2', '5':'1', '6':'0'}             

my_filtered_csv['expression'] = my_filtered_csv['expression'].replace(emotion_map)         

print(my_filtered_csv)      

I end up with the following error log:

Traceback (most recent call last):
  File "/Users/mona/CS585/project/affnet/emotion_map.py", line 11, in <module>
    my_filtered_csv['expression'] = my_filtered_csv['expression'].replace(emotion_map)
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/generic.py", line 3836, in replace
    limit=limit, regex=regex)
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/generic.py", line 3885, in replace
    regex=regex)
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/internals.py", line 3259, in replace_list
    masks = [comp(s) for i, s in enumerate(src_list)]
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/internals.py", line 3259, in <listcomp>
    masks = [comp(s) for i, s in enumerate(src_list)]
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/internals.py", line 3247, in comp
    return _maybe_compare(values, getattr(s, 'asm8', s), operator.eq)
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/internals.py", line 4619, in _maybe_compare
    raise TypeError("Cannot compare types %r and %r" % tuple(type_names))
TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'

Process finished with exit code 1

May 30 in Python by Ashish
22 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Hey @Ashish, change the emotion_map to the following:

​emotion_map = { 0:6, 1:3, 2:4, 3:5, 4:2, 5:1, 6:0}

It helped me

answered May 30 by Mir

Related Questions In Python

0 votes
1 answer

Python error: unsupported operand type(s) for -: 'str' and 'str'

Hi. This error is caused because you ...READ MORE

answered May 12 in Python by Anjali
51 views
0 votes
1 answer

Python error "TypeError: Can't convert 'int' object to str implicitly"

Hey Yamini, you cannot concatenate a string ...READ MORE

answered May 31 in Python by Gargi
30 views
0 votes
1 answer

When I create and remove files rapidly on windows using python I get WindowsError (Error 5)

Here's the short answer: disable any antivirus or ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,710 points
76 views
0 votes
1 answer

Why am I getting a error when I am printing two different data types in python?

different data type is being used. that ...READ MORE

answered Mar 6 in Python by Mohammad
• 1,400 points
19 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
56 views
+4 votes
6 answers
0 votes
1 answer
0 votes
1 answer

Is there a way to list out in-built variables and functions of Python?

The in-built variables and functions are defined ...READ MORE

answered May 14 in Python by Junaid
16 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.