python - 具有不可散列类型的列的唯一性

标签 python pandas

我的三分之二的专栏对 .nunique() react 良好

print(df.Column1.nunique())
>>> 45
print(df.Column2.nunique())
>>> 23

但另一栏:

print(df.Column3.nunique())

..给出一个回溯,最后两行如下:

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.unique (pandas/hashtable.c:14999)()

TypeError: unhashable type: 'RequestsCookieJar'

当你在 Jupyter 中查看 df 时,你可以在某种程度上直观地看到 cookie 之间的差异,可能会这样开始:

[<Cookie 1P_JAR=2018-03-07-09

..其他像这样:

[<Cookie 1P_JAR=2018-03-07-08

是否有一个快速修复方法可以告诉您其中有多少是唯一的?

最佳答案

看来需要:

print(df.Column3.astype(str).nunique())

或者:

print(df.Column3.apply(tuple).nunique())

关于python - 具有不可散列类型的列的唯一性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49154732/

相关文章:

python - 在 python 2.7 中打开空格(?)分隔的文本文件?

Python Pandas : How to Insert one Missing Row?

python - 使用 Pandas DateOffset 移动到给定时间的最近过去的日期时间

python - 查找一个序列并删除之前的条目

带有 __ 的 Python 'hide' 方法

python - 在 Python 多处理中将 Pool.map 与共享内存数组结合起来

python - Keras 使用 Lambda 层错误与 K.ctc_decode

python - 值错误 : bad input shape in sklearn Python

python - 处理错误 "TypeError: Expected tuple, got str"将 CSV 加载到 pandas 多级和多索引 (pandas)

python - 如何使用 tsplot 设置多个标记?