-1

I have 2 columns (input & target) in the pandas which includes the list. The purpose is to find how many common item in these 2 lists and save the result in the new column "common"

For the 1st row, only have 'ELITE' in common. For the 2nd row, both 'ACURAWATCH' & 'PLUS' exist in both list.

Input:

frame = pd.DataFrame({'input' : [['MINIVAN','TOURING','ELITE'], ['4D','SUV','ACURAWATCH','PLUS']], 'target' : [['MINI','TOUR','ELITE'], ['ACURAWATCH','PLUS']]})

Expect Output:

frame = pd.DataFrame({'input' : [['MINIVAN','TOURING','ELITE'], ['4D','SUV','ACURAWATCH','PLUS']], 'target' : [['MINI','TOUR','ELITE'], ['ACURAWATCH','PLUS']], 'common' :[1, 2]})

3 Answers3

1

You can use set.intersection with df.apply:

In [4307]: frame["common"] = frame.apply(
lambda x: len(set(x["input"]).intersection(set(x["target"]))), 1)

In [4308]: frame
Out[4308]: 
                         input               target  common
0    [MINIVAN, TOURING, ELITE]  [MINI, TOUR, ELITE]       1
1  [4D, SUV, ACURAWATCH, PLUS]   [ACURAWATCH, PLUS]       2
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
0

You can apply a custom function with np.intersect1d:

frame['common'] = frame.apply(lambda x: len(np.intersect1d(x['input'],x['target'])), axis=1)
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

You could use:

frame['common'] = [len(set(x) & set(y)) for x, y in frame.to_numpy()]
print(frame)

Output

                         input               target  common
0    [MINIVAN, TOURING, ELITE]  [MINI, TOUR, ELITE]       1
1  [4D, SUV, ACURAWATCH, PLUS]   [ACURAWATCH, PLUS]       2
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76