我有一个数据: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().
这可能是什么原因?
pd.notna([a, b])
返回真/假值数组。 all
将迭代该数组,但每个迭代值都是一整行,而不是单个值。问题在于这个迭代数组的真值。让我们说foo = pd.notna([a, b])
。 foo[0]
是一个数组,bool(foo[0])
是不明确的。
您可以通过以下方式解决问题:
pd.notna([a, b]).all()