8

I have this simple dataframe

Num Fruit Price 1 Apple 1.00 1 Apple 1.00 2 Apple 1.50 2 Orange 1.50 3 Orange 1.00 3 Banana 0.50 

I want to drop all the rows which have the fruit Apple or Orange

The expected output should be like this:

Num Fruit Price 3 Banana 0.50 

I tried to doing the following syntax, but somehow it did not drop all the rows in the dataframe

>>> df.drop(df.Fruit.isin(["Apple","Orange"])) Fruit Num Price 2 Apple 2 1.50 3 Orange 2 1.50 4 Orange 3 1.00 5 Banana 3 0.50 

Any suggestion how to solve this?

1 Answer 1

18

You need to pass the indices of the rows to be dropped, but you are passing a boolean array. You can change it to:

df.drop(df[df.Fruit.isin(["Apple", "Orange"])].index) Out: Num Fruit Price 5 3 Banana 0.5 

Or you can select the rows that don't contain apple or orange:

df[~(df.Fruit.isin(["Apple", "Orange"]))] Out: Num Fruit Price 5 3 Banana 0.5 
Sign up to request clarification or add additional context in comments.

1 Comment

Note that you should pass the argument inplace=True to drop in order to mutate the dataframe rather than returning a view with the rows dropped.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.