具有不可变类型的列的唯一

时间:2018-03-07 14:49:48

标签: python pandas

我的三列中有两列对.nunique()

的反应很好
print(df.Column1.nunique())
>>> 45
print(df.Column2.nunique())
>>> 23

但是另一栏:

print(df.Column3.nunique())

..给出一个Traceback,其中包含以下最后两行:

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

是否有快速修复,以便它可以告诉您有多少是独一无二的?

1 个答案:

答案 0 :(得分:1)

似乎需要:

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

或者:

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