bbartling
bbartling

Reputation: 3494

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

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?

Upvotes: 4

Views: 4527

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

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()

Upvotes: 9

Related Questions