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

时间:2021-04-19 14:48:56

标签: python pandas

我有一个包含 SQL 的“标签”的 Pandas 数据框列,我很想知道这些标签的唯一值是什么。

对于我的 Pandas 数据框列,如果我使用 tags.m_tags.unique() 这将通过错误 TypeError: unhashable type: 'list'

手动查看数据 m_tags 列表格式如下:

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

有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

只需使用 explode() 方法并将 unique() 方法链接到它:

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

现在如果你打印 result 你会得到你想要的输出

编辑:如果您有字典,请使用:

result=df['tags'].apply(lambda x:list(x.values())).explode().unique()