import pandas as pd
df = pd.DataFrame({"col1": ["a", "b", "c", ["a", "b"]]})
我有一个像这样的数据框,我想在该列中找到包含列表的行。我尝试了value_counts(),但是花了这么长时间,最后抛出错误。这是错误:
TypeError Traceback (most recent call last)
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.map_locations()
TypeError: unhashable type: 'list'
Exception ignored in: 'pandas._libs.index.IndexEngine._call_map_locations'
Traceback (most recent call last):
File "pandas/_libs/hashtable_class_helper.pxi", line 1709, in pandas._libs.hashtable.PyObjectHashTable.map_locations
TypeError: unhashable type: 'list'
c 1
a 1
[a, b] 1
b 1
Name: col1, dtype: int64
对于更大的数据帧,这将永远花费。
这是所需输出的样子:
col1
c 1
b 1
[a,b] 1
dtype: int64
答案 0 :(得分:3)
在以下条件下对行进行迭代并检查列中obj
的类型:type(obj) == list
import pandas as pd
df = pd.DataFrame({"col1": ["a", "b", "c", ["a", "b"]]})
for ind in df.index:
print (type(df['col1'][ind]) == list)
结果如下:
False
False
False
True
答案 1 :(得分:2)
列表是可变的,无法比较,因此您既不能计算值,也不能将它们设置为索引。您需要转换为tuple
或(要感谢@CameronRiddell)才能计数:set
df['col1'].apply(lambda x: tuple(x) if isinstance(x, list) else x).value_counts()
输出:
c 1
b 1
a 1
(a, b) 1
Name: col1, dtype: int64