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
65 views

1 answer to this question.

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
177 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
61 views
0 votes
0 answers

i am normalizing the data set iris in python and get the error ::TypeError: 'numpy.float64' object is not callable

for i in cnames:     print(i)     df_csv[i] = (df_csv[i] - ...READ MORE

2 days ago in Python by anonymous
5 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
83 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 122 views
+4 votes
6 answers
0 votes
1 answer

Python error "TypeError: string indices must be integers, not str"

Hey @Dipti email_s.append(email_1["email_address"]) This is the list on ...READ MORE

answered Jul 5 in Python by Jinu
33 views
0 votes
1 answer