我想计算A列和B列相交的实例数。 A列和B列中的行是字符串列表。 例如,列A可以包含[汽车,乘客,卡车],列B可以包含[汽车,房屋,花卉,卡车]。因为在这种情况下,2个字符串重叠,列C应该显示 - > 2
我尝试过(这些都不起作用):
df['unique'] = np.unique(frame[['colA', 'colB']])
或
def unique(colA, colB):
unique1 = list(set(colA) & set(colB))
return unique1
df['unique'] = df.apply(unique, args=(df['colA'], frame['colB']))
TypeError :('unique()需要2个位置参数,但3个被赋予','发生在索引文章')
答案 0 :(得分:7)
我认为在列表理解中length
需要df['C'] = [len(set(a).intersection(b)) for a, b in zip(df.A, df.B)]
:
df['C'] = [len(set(a) & set(b)) for a, b in zip(df.A, df.B)]
或者:
df = pd.DataFrame(data={'A':[['car', 'passenger', 'truck'], ['car', 'truck']],
'B':[['car', 'house', 'flower', 'truck'], ['car', 'house']]})
print (df)
A B
0 [car, passenger, truck] [car, house, flower, truck]
1 [car, truck] [car, house]
df['C'] = [len(set(a).intersection(b)) for a, b in zip(df.A, df.B)]
print (df)
A B C
0 [car, passenger, truck] [car, house, flower, truck] 2
1 [car, truck] [car, house] 1
<强>示例强>:
return new RedirectResponse('persons', array(), UrlGeneratorInterface::RELATIVE_PATH);