1

Dataframe in question, df:

colA colB
1  [1, 4, 5]
4  [3, nan, nan]

I'm trying to return a Series which has True where colA's value is in colB's value for each row.

The result should be:

True
False

I tried: df.colA.isin(df.colB) - but that doesn't do the trick because colB's values are in lists

3
  • 1
    df.apply(lambda x: x['colA'] in x['colB'], axis=1) Commented Sep 30, 2020 at 21:57
  • or [a in b for a, b in zip(df['colA'], df['colB'])] Commented Sep 30, 2020 at 21:59
  • Great that works, thanks! First one is good because it's memory efficient Commented Sep 30, 2020 at 22:00

1 Answer 1

1

You need to unpack the list columns before isin

m = pd.DataFrame(df['colB'].tolist(),index=df.index).isin(df['colA']).any(axis=1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.