0

I have a pandas dataframe that looks like the one below, and I want to drop several labels.

What works fine is:

df = df[df['label'] != 'A']

or:

df = df[(df['label'] != 'A') & (df['label'] != 'B')]

However, I have many labels that I want to drop, so I am looking for a command that works like:

df = df[df['label'] != ['A','B']]

But this doesn't work

How can I drop, or the reverse select, several rows with different labels in one command? I am using pandas only for a few weeks now but can't find an answer to this problem.

    label   value1  value2  value3
0    A   63.923445   109.688995 -0.692308
1    A   42.488038   87.081340  -0.692308
2    A   45.167464   91.267943  -0.504808
3    A   48.229665   88.755981  -1.485577
4    B   78.010000   180.100000  3.710000
5    B   87.833333   183.800000  2.225000
6    B   93.820000   181.980000  3.460000
7    B   110.836667  221.806667  2.833333
8    C   48.750000   127.450000  NaN
9    C   43.950000   103.100000  NaN
10   C   NaN     74.970000   NaN
11   D   27.800000   89.250000   3.550000
12   D   28.000000   92.080000   3.530000
13   E   61.400000   99.300000   NaN
14   E   95.600000   257.000000  NaN
15   E   49.800000   145.000000  NaN
16   G   64.710000   136.000000  1.160000
Koen Kramer
  • 11
  • 1
  • 3
  • possible duplicate of [Remove rows not .isin('X')](http://stackoverflow.com/questions/14057007/remove-rows-not-isinx) – mkln Feb 06 '14 at 10:52

1 Answers1

0

try this:

import numpy as np
df = df[np.logical_not(df['label'].isin(['A','B']))]

or

df = df[- df['label'].isin(['A', 'B'])]

see Remove rows not .isin('X')

Community
  • 1
  • 1
mkln
  • 14,213
  • 4
  • 18
  • 22