Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas .unique() TypeError: unhashable type: 'list'

Tags:

python

pandas

I have a pandas dataframe column that contains "tags" for SQL and I was curious to see what unique values would be for these tags.

For my pandas dataframe column if I use tags.m_tags.unique() this will through an error TypeError: unhashable type: 'list'

Manually looking at the data m_tags it is in list format looks like this:

[reheat, cmd]
[discharge, temp, air, sensor]
[flow, air, sensor]
[zone, temp, air, sensor]

Would anyone know how to get around this?

like image 775
bbartling Avatar asked Oct 31 '25 19:10

bbartling


1 Answers

Just use explode() method and chain unique() method to it:

result=tags['m_tags'].explode().unique()

Now if you print result you will get your desired output

EDIT : If you have dictionary then use:

result=df['tags'].apply(lambda x:list(x.values())).explode().unique()
like image 129
Anurag Dabas Avatar answered Nov 02 '25 07:11

Anurag Dabas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!