Prajit Karande
Prajit Karande

Reputation: 131

How to get list of id's from one column which not contains in another column in pandas

pandas dataframe have two column which have list of id's need to get id,s which not contain in another columns

id  Column_1    Column_2
1   [1,2,5,7,9] [1,2,5,7]
2   [4,8,2,7]   [4,8,2,7]
3   [5,7,2,9]   [9] 
4   [4,7,2,9]   [3]


I want to  result like
id  Column_1    Column_2    result
1   [1,2,7]     [1,2,5,7,9] [5,9]
2   [4,8,2,7]   [4,8,2,7]   []
3   [5,7,2,9]   [9]         []
4   [4,7,2,9]   [3]         [3]

Upvotes: 3

Views: 286

Answers (1)

jezrael
jezrael

Reputation: 863166

Convert values to sets and get difference:

df['Column_3'] = [list(set(y).difference(x)) for x, y in zip(df['Column_1'], df['Column_2'])]
print (df)
   id      Column_1         Column_2 Column_3
0   1     [1, 2, 7]  [1, 2, 5, 7, 9]   [9, 5]
1   2  [4, 8, 2, 7]     [4, 8, 2, 7]       []
2   3  [5, 7, 2, 9]              [9]       []
3   4  [4, 7, 2, 9]              [3]      [3]

Upvotes: 1

Related Questions