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, 2019 in Python by Ashish
8,135 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, 2019 by Mir

Related Questions In Python

0 votes
2 answers

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

A TypeError can occur if the type ...READ MORE

answered Feb 5, 2020 in Python by lovelmark
• 160 points
1 flag 19,307 views
0 votes
1 answer

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

TRY THIS   #Nomalisation for i in names:     print(i)   ...READ MORE

answered Aug 20, 2019 in Python by Noel Deepak Palle
5,877 views
0 votes
1 answer
0 votes
1 answer

Python: TypeError :Cannot cast Index to dtype int

Try to use numpy  and use .astype(np,int) ...READ MORE

answered Jun 25, 2020 in Python by Sirajul
• 59,230 points
7,101 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,415 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,026 views
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, 2019 in Python by Jinu
22,936 views
0 votes
1 answer

What are the naming conventions for variables and data types in python?

There are certain rules that we have ...READ MORE

answered May 21, 2019 in Python by Mohammad
• 3,230 points
2,069 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP