0

How should I fix this?

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)                                                                     

Error is:

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

A few lines of the csv file looks like:

,subDirectory_filePath,expression
0,689/737db2483489148d783ef278f43f486c0a97e140fc4b6b61b84363ca.jpg,1
1,392/c4db2f9b7e4b422d14b6e038f0cdc3ecee239b55326e9181ee4520f9.jpg,0
2,468/21772b68dc8c2a11678c8739eca33adb6ccc658600e4da2224080603.jpg,0
3,944/06e9ae8d3b240eb68fa60534783eacafce2def60a86042f9b7d59544.jpg,1
4,993/02e06ee5521958b4042dd73abb444220609d96f57b1689abbe87c024.jpg,8
5,979/f675c6a88cdef99a6d8b0261741217a0319387fcf1571a174f99ac81.jpg,6
6,637/94b769d8e880cbbea8eaa1350cb8c094a03d27f9fef44e1f4c0fb2ae.jpg,9
7,997/b81f843f08ce3bb0c48b270dc58d2ab8bf5bea3e2262e50bbcadbec2.jpg,6
8,358/21a32dd1c1ecd57d3e8964621c911df1c0b3348a4ae5203b4a243230.JPG,9

2 Answers 2

1

Changing the emotion_map to the following fixed the problem:

emotion_map = { 0:6, 1:3, 2:4, 3:5, 4:2, 5:1, 6:0}
1
  • Thanks a lot, this solution saved my day! Never really considered the dictionary might be the problem.
    – gilgamash
    May 9, 2019 at 13:05
0

Another possiblity which can also create this error is: you have already run this code once and the data is already replaced.

To solve this problem, you can go back and load the data set again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.