在Python Pandas中查找两列的交集 - >字符串列表

时间:2018-04-12 12:14:34

标签: python list pandas unique

我想计算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个被赋予','发生在索引文章')

1 个答案:

答案 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);