22

I looked at the unique values in a column of a dataframe - pandas that I have. And there are some names in one of the columns that I do not want to include, how do I remove those rows from the dataframe, without using index value notation, but by saying if row value = "this" then remove

like...

new = df.copy

df['some column'].drop_values('this','that','other')
yoshiserry
  • 20,175
  • 35
  • 77
  • 104

1 Answers1

44

See indexing with isin (also, boolean indexing):

mask = df['some column'].isin(['this', 'that', 'other'])
df[~mask]
behzad.nouri
  • 74,723
  • 18
  • 126
  • 124
  • Thanks @Behzad! does the [~idx] notation, mean show everything in the dataframe which are not in variable, idx? Is this also another way to do it? df[df.line_race != 0] http://stackoverflow.com/questions/18172851/deleting-dataframe-row-in-pandas-based-on-column-value – yoshiserry Mar 13 '14 at 23:08
  • 1
    @yoshiserry see [Boolean indexing](http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing) – behzad.nouri Mar 13 '14 at 23:09
  • 1
    @yoshiserry also see [indexing with isin](http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-with-isin) – behzad.nouri Mar 13 '14 at 23:11