python - 当值是列表时,在 Pandas 中获取唯一值
问题描述
我有一个 DF,其中一列包含一系列列表。
In [64]: df[~df['packet/net/sourceRoute'].isnull()]['packet/net/sourceRoute']
Out[64]:
2177 [fd00::2]
2178 [fd00::2]
2182 [fd00::2]
3860 [fd00::2, fd00::3]
3861 [fd00::2, fd00::3]
...
21329 [fd00::8]
21331 [fd00::7, fd00::8]
21354 [fd00::8]
21355 [fd00::8]
21358 [fd00::8]
Name: packet/net/sourceRoute, Length: 105, dtype: object
我想获取该列的值packet/net/sourceRoute
。但是,如果我确实应用该unique()
方法,我会收到此错误。
In [70]: df['packet/net/sourceRoute'].unique()
---------------------------------------------------------------------------
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.unique()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable._unique()
TypeError: unhashable type: 'list'
即使我尝试删除重复项,我也做不到。
In [73]: df[~df['packet/net/sourceRoute'].isnull()]['packet/net/sourceRoute'].drop_duplicates()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: unhashable type: 'list'
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
<ipython-input-73-fef53b94129b> in <module>
----> 1 df[~df['packet/net/sourceRoute'].isnull()]['packet/net/sourceRoute'].drop_duplicates()
SystemError: <built-in function duplicated_object> returned a result with an error set
有任何想法吗?
谢谢!
解决方案
推荐阅读
- swift - Swift5如何将函数传递给向量/函数
- python - 具有多个元素的数组的真值是不明确的。使用多个条件时使用 a.any() 或 a.all()
- image - 简单机器学习图像分类模型的最佳实践 RESTful API 命名约定
- sql - 1 如果值不是 NULL,否则为 NULL (Oracle)
- ios - 使用没有返回的 Swift 组合的 POST 请求不适用于 AnyPublisher
- java - 带有弹簧启动的天蓝色应用程序服务:等待对预热的响应
- linux - fIm 试图在两个匹配模式之间找到一个字符串,然后使用 sed 在一个模式之前添加该字符串
- flutter - flt_telephony_info 得到空结果
- apache-kafka - Google Cloud Spanner 通过 Cloud Data Fusion 或其他方式实时更改数据捕获到 PubSub/Kafka
- python - 如何动态确定要创建哪个 Python 子类?