-1

I'm working on a dataframe and I want to drop some rows with multiple specific string value on column. For example, the df is like this:

 ID     type           price
da499   hotel         $41946
fa987   hotel         $251
gh552   Restaurant    $764
vc947   bar           $2314
bc521   bar           $2191
fv231   Restaurant    $4985
df987   estate        $654
bv231   estate        $231
kc818   school        $91456

And I want to drop the rows with type equal to hotel, Restaurant and estate to form a df like this:

 ID     type           price
vc947   bar           $2314
bc521   bar           $2191
kc818   school        $91456

How can I use the drop function to get the result?

Zephyr
  • 11,891
  • 53
  • 45
  • 80
kali
  • 117
  • 7

1 Answers1

0

You can use pandas.DataFrame.isin() method to get the row where type is equal to 'hotel', 'Restaurant' or 'estate'. Then you can reverse the boolean filter with ~.

import pandas as pd

df = pd.read_csv('data.csv')

df = df[~df['type'].isin(['hotel', 'Restaurant', 'estate'])]
          ID    type   price
    3  vc947     bar   $2314
    4  bc521     bar   $2191
    8  kc818  school  $91456

Alternatively, if you want to use pandas.DataFrame.drop() method, you can do:

df = df.drop(df.index[df['type'].isin(['hotel', 'Restaurant', 'estate'])])
          ID    type   price
    3  vc947     bar   $2314
    4  bc521     bar   $2191
    8  kc818  school  $91456

Although, for performance reason, the former is faster.

Zephyr
  • 11,891
  • 53
  • 45
  • 80