Мы можем сделать explode
с groupby
, создать целое list
в каждом index
, затем выполнить set
sub
df['l']=df.col1.str.split(',')
df['new']=df.explode('l').groupby('index')['l'].agg(list).reindex(df['index']).tolist()
df['List']=(df.new.apply(set)-df['l'].apply(set)).apply(list)
df.loc[~df.List.astype(bool),'List']=df.l
df
index col1 l new List
0 1 a [a] [a, b, c] [c, b]
1 1 b [b] [a, b, c] [a, c]
2 1 c [c] [a, b, c] [a, b]
3 2 d [d] [d, e, f] [e, f]
4 2 e, f [e, f] [d, e, f] [d]
5 3 g [g] [g] [g]
Обновить
l=[]
... for x , y in zip(df.l,df.new):
... x=x.copy()
... y=y.copy()
... for i in x:
... if i in y:
... y.remove(i)
... l.append(y)
...
l
[['b', 'c'], ['a', 'c'], ['a', 'b'], ['e', ' f'], ['d'], []]
df['List']=l