在Python Pandas中找到两列的交集->字符串列表 [英] Find intersection of two columns in Python Pandas -> list of strings

查看:56
本文介绍了在Python Pandas中找到两列的交集->字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算A和B列相交的实例数量.列A和B中的行是字符串列表.例如,列A可以包含[汽车,乘客,卡车],列B可以包含[汽车,房屋,花卉,卡车].由于在这种情况下,两个字符串重叠,因此C列应显示-> 2

I would like to count how many instances of column A and B intersect. The rows in Column A and B are lists of strings. For example, column A may contain [car, passenger, truck] and column B may contain [car, house, flower, truck]. Since in this case, 2 strings overlap, column C should display -> 2

我已经尝试过(这些工作都没有):

I have tried (none of these work):

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个位置参数",发生在索引文章上")

TypeError: ('unique() takes 2 positional arguments but 3 were given', 'occurred at index article')

推荐答案

我认为需要 length

I believe need length with set.intersection in list comprehension:

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

这篇关于在Python Pandas中找到两列的交集->字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆