简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM