HELLOWORLD
HELLOWORLD

Reputation: 13

pandas isin based on a single row

Now I have:

ss    dd    list
A     B     [B,E,F]
C     E     [C,H,E]
A     C     [A,D,E]

I want to rule out rows that both ss and dd are in list. So we rule out row 2. Function isin() checks if ss and dd are in all rows of list each time, which is not giving me the result.

Please do not use loop cause my dataset is too large. Output should be:

ss    dd    list
A     B     [B,E,F]
A     C     [A,D,E]

Upvotes: 0

Views: 273

Answers (1)

BENY
BENY

Reputation: 323376

First we flatten your list column to a dataframe and using isin(here index is do matter , that is why I using original dataframe index to create the cdf)

cdf=pd.DataFrame(df['list'].tolist(),index=df.index)
mask=(cdf.isin(df.ss).any(1))&(cdf.isin(df.dd).any(1))
df[~mask]
Out[589]: 
  ss dd       list
0  A  B  [B, E, F]
2  A  C  [A, D, E]

Upvotes: 1

Related Questions