0

using pandas in Python2-7 I have the following dataframe (dfmain):

    Col1    Letter
0   One     A
1   Two     B
2   Three   C
3   Four    A
4   Five    D
5   Six     B
6   Seven   C

I'd like to filter out all rows with 'C' or 'D' as a letter or alternatively only rows 'A' and 'B'. The following code works for only one letter:

dftemp = dfmain[dfmain['Letter'] == 'A']

or alternatively:

dftemp = dfmain[dfmain['Letter'] != 'D']

However, how would I do this for more than one letter, e.g. I've tried:

dftemp = dfmain[dfmain['Letter'] == 'A', 'B']

or:

dftemp = dfmain[dfmain['Letter'] == ['A','B']]

and a few other ways but these produce errors. Any ideas?

Oaka13
  • 143
  • 1
  • 1
  • 8
  • 1
    Possible duplicate of [How to implement 'in' and 'not in' for Pandas dataframe](http://stackoverflow.com/questions/19960077/how-to-implement-in-and-not-in-for-pandas-dataframe) – Alex Riley Jan 04 '16 at 11:39
  • As per the linked question, you can use `dfmain[dfmain['Letter'].isin('A', 'B'])]` – Alex Riley Jan 04 '16 at 11:40

1 Answers1

1

Use isin to test for membership of multiple values:

In [10]:
dftemp = df[df['Letter'].isin(['A', 'B'])]
dftemp

Out[10]:
   Col1 Letter
0   One      A
1   Two      B
3  Four      A
5   Six      B
EdChum
  • 376,765
  • 198
  • 813
  • 562