1

For the following data frame I want to filter data based on count value. I want to keep count 10, 5, and 2:

index      count
    1           10
    2           5
    3           2
    4           1
    5           6
    6           7
    7           "tab"

I know that I can write the code as

df[(df.count==10) | (df.count==5) | (df.count==2) |  (df.count=="tab")]

Is there any simpler way to do it? I have more than 20 values. I tried the following, but it did not work:

df[(df.count==[10, 5, 2, "tab"])

Thank you.

Mary
  • 1,142
  • 1
  • 16
  • 37
  • `df.count` will not work, use `df['count']` (`count` is a function, `.count` will access the function, not the column). – cs95 Aug 29 '19 at 03:37

1 Answers1

1

Use isin:

df[df['count'].isin([10, 5, 2, "tab"])]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114