Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较python数据帧中2列的列表,并计算相同的项_Python_List_Dataframe - Fatal编程技术网

比较python数据帧中2列的列表,并计算相同的项

比较python数据帧中2列的列表,并计算相同的项,python,list,dataframe,Python,List,Dataframe,如何比较python数据帧中两列的列表,并计算数据帧中这两列之间的相同列表。例如: column A | column B ==================================== ['a', 'b', 'c'] | ['a', 'b'] ['a', 'b'] | ['a'] ['b'] | ['a'] 我想得到这个结果: column A | column B

如何比较python数据帧中两列的列表,并计算数据帧中这两列之间的相同列表。例如:

column A            |   column B
====================================
['a', 'b', 'c']     | ['a', 'b']
['a', 'b']          | ['a']
['b']               | ['a']  
我想得到这个结果:

    column A            |   column B    | count_same_item
    ======================================================
    ['a', 'b', 'c']     | ['a', 'b']    | 2
    ['a', 'b']          | ['a']         | 1
    ['b']               | ['a']         | 0
非常感谢您的帮助

试试这个:

df['count_same_item'] = df.apply(lambda x: len(set(x['column A']) & set(x['column B'])), axis=1)
print(df)
输出:

    column A column B  count_same_item
0  [a, b, c]   [a, b]                2
1     [a, b]      [a]                1
2        [b]      [a]                0
试试这个:

df['count_same_item'] = df.apply(lambda x: len(set(x['column A']) & set(x['column B'])), axis=1)
print(df)
输出:

    column A column B  count_same_item
0  [a, b, c]   [a, b]                2
1     [a, b]      [a]                1
2        [b]      [a]                0