简体   繁体   中英

Compare list of 2 columns in a python dataframe and count the same item

how to compare list of 2 columns in a python dataframe and count the same list between these 2 column in a dataframe. For example:

column A            |   column B
====================================
['a', 'b', 'c']     | ['a', 'b']
['a', 'b']          | ['a']
['b']               | ['a']  

i want to get this result:

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

really appreciate any help

Try this:

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

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM