bo_
bo_

Reputation: 81

How to delete the selected rows from the dataframe

I have a 121 rows × 20 columns dataframe
I want to delete the rows from the picture below selected rows

I had tried :

sv.drop(sv[(sv['Visit_Duration'] == 'Never')  &  (sv['Visit_Plan'] == 'never')].index,axis=1)   

It turned out error

SO I tried

sv.drop(labels=[44,67,107,108,112],axis=0)

BUT the result are the same as the original dataframe with 121 rows x 20 columns

Upvotes: 0

Views: 1020

Answers (2)

user7864386
user7864386

Reputation:

Why even delete them? Just filter them out. You want to keep the rows that don't satisfy the conditions you specified here, so you want the negation of A&B, which is ~A|~B:

sv = sv[(sv['Visit_Duration'] != 'Never')  |  (sv['Visit_Plan'] != 'never')]

or equivalently (~(A&B)),

sv = sv[~((sv['Visit_Duration'] == 'Never')  &  (sv['Visit_Plan'] == 'never'))]

Upvotes: 3

kelyen
kelyen

Reputation: 242

You're almost there, you forget to reassign the result to the dataframe variable.

Try instead:

sv = sv.drop(labels=[44,67,107,108,112],axis=0)

Or you can also use the inplace=True argument for the drop function.

sv.drop(labels=[44,67,107,108,112], axis=0, inplace=True)

Upvotes: 3

Related Questions