0

My DataFrame:

My Dataframe

Trying...

Does not work

Nope...

Does not work

Still nope...

AND YET...

Huh?

Any idea why I can't filter this dataframe by a list using isin?

I'm expecting it to return one or more rows where the Chord Notes column equals the list ['C','E','G']

5
  • 2
    Don't make us retype code from an image. Please post all code, input, output, and error messages as plain text. Commented Mar 24, 2023 at 1:31
  • 1
    reason being that column contains a actual list objects Commented Mar 24, 2023 at 1:33
  • You need to put the list ['C', 'E', 'G'] inside a list, like [['C', 'E', 'G']] Commented Mar 24, 2023 at 1:34
  • Anurag, so how would you solve? Commented Mar 24, 2023 at 1:35
  • Thank you lucas, your answer was correct as well Commented Mar 24, 2023 at 1:45

1 Answer 1

2

you can still use isin() but since the values under 'Chord Notes' are actual list object so you have to pass that list object i.e ['C','E','G'] in isin() method

output = full_chords[full_chords['Chord Notes'].isin([['C','E','G']])]

Or with map() (kind of loop under the hood):

output = full_chords[full_chords['Chord Notes'].map(lambda x:x==['C','E','G'])]

Or

Other way around is to typecast that column to string(not recommended but still an option tho):

output = full_chords[full_chords['Chord Notes'].astype(str).eq("['C','E','G']")]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this worked perfectly and the logic makes sense.

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.