我有一个数据是:https://github.com/mayuripandey/Data-Analysis/blob/main/Topic.csv,
例如:
Topic1_assignment | Topic2_assignment
0 Int64Index([ 0, 1, 3, 7, 8, 11], dtype='int64 | Int64Index([ 0, 4, 5, 9, 11, 14], dtype='int64)
1 NaN | Int64Index([ 0, 2, 5, 7, 10, 14], dtype='int64)
2 Int64Index([ 0, 1, 2, 210, 219, 221], dtype='int64') |. Int64Index([ 256, 257, 258, 259, 260, 261], dtype='int64)
在这里我试图找到两个包含NaN值的列表的交集。
我使用的代码是:
df9['c'] = [len(set(a).intersection(b)) if all(pd.notna([a, b])) else 0
for a, b in zip(df9.Topic1_assignment, df9.Topic2_assignment)],
但它给出了一个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-65-af99ddc88358> in <module>
----> 1 df9['c'] = [len(set(a).intersection(b)) if all(pd.notna([a, b])) else 0
2 for a, b in zip(df9.Topic1_assignment, df9.Topic2_assignment)]
<ipython-input-65-af99ddc88358> in <listcomp>(.0)
----> 1 df9['c'] = [len(set(a).intersection(b)) if all(pd.notna([a, b])) else 0
2 for a, b in zip(df9.Topic1_assignment, df9.Topic2_assignment)]
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
可能的原因是什么?
1条答案
按热度按时间z4iuyo4d1#
pd.notna([a, b])
返回一个true/false值数组。all
将迭代该数组,但每个迭代值都是一整行,而不是单个值。问题在于此迭代数组的真值。假设foo = pd.notna([a, b])
。foo[0]
是一个数组,而bool(foo[0])
不明确。您可以通过以下方法解决问题