0
$\begingroup$

I have the following dataframe comments. I have segregated a list of users based on certain conditions. I want to get the count of words based on those users which are named as gold_users. But I am getting an error in my code TypeError: unhashable type: 'list'. Please help me fix this.

DataFrame(comments)

       Id|                    Text                             |    UserId  
        6|  [2006, course, allen, knutsons, 2001, course, ...  |    3   
        8|  [also, theo, johnsonfreyd, note, mark, haimans...  |    1

Code

for index,rows in comments.iterrows():
  gold_comments = rows[comments.Text.loc[comments.UserId.isin(gold_users)]]
  Counter(gold_comments)

Expected Output

#Top 10 Words that appear the most in the comments made by gold users with their count.
 [['scholar',20],['school',18],['bus',15],['class',14],['teacher',14],['bell',13],['time',12],['books',11],['bag',9],'student',7]]
$\endgroup$
1
$\begingroup$
TypeError: unhashable type: 'list'.

You will get this error when you are trying to put list as key in dictionary or set because list is unhashable object.

Example you trying to input code such as

dict1 ={ 1:'one', [2]:'two'}
print(dict1)

O/p:
TypeError Traceback (most recent call last)
<ipython-input-73-c4e2fd1e6bf0> in <module>
----> 1 dict1 ={ 1:'one', [2]:'two'}
      2 print(dict1)
 
TypeError: unhashable type: 'list'

solution : Try to convert the list into tuple

Hashable object in Python:

  • int
  • float
  • decimal
  • bool
  • string
  • tuple
  • complex
  • range
  • frozenset
  • bytes

UnHashable object in Python

  • list
  • set
  • dict
  • bytearray
  • customclasses
| improve this answer | |
$\endgroup$

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