pandas .unique() TypeError:不可散列的类型:“列表”

Hen*_*Hub 3 python pandas

我有一个 pandas 数据框列,其中包含 SQL 的“标签”,我很好奇这些标签的唯一值是什么。

对于我的 pandas 数据框列,如果我使用tags.m_tags.unique()它会出现错误TypeError: unhashable type: 'list'

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

[reheat, cmd]
[discharge, temp, air, sensor]
[flow, air, sensor]
[zone, temp, air, sensor]
Run Code Online (Sandbox Code Playgroud)

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

Anu*_*bas 8

只需使用explode()方法和链unique()方法即可:

result=tags['m_tags'].explode().unique()
Run Code Online (Sandbox Code Playgroud)

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

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

result=df['tags'].apply(lambda x:list(x.values())).explode().unique()
Run Code Online (Sandbox Code Playgroud)