1

I want to delete some rows in a pandas DataFrame.

    id  marks
 1  123 45
 2  124 67
 3  127 89
 4  257 10
 5  345 34

delRows = [123,127]

output should be -

    id  marks
 2  124 67
 4  257 10
 5  345 34

Can anyone tell how to do it?

df = df[df.id != delRows]

Is it the right way to do it???

0

1 Answer 1

2

You can try isin with inverted mask by ~:

print df
    id  marks
1  123     45
2  124     67
3  127     89
4  257     10
5  345     34

delRows = [123,127]

print df.id.isin(delRows)
1     True
2    False
3     True
4    False
5    False
Name: id, dtype: bool

print ~df.id.isin(delRows)
1    False
2     True
3    False
4     True
5     True
Name: id, dtype: bool

print df[~df.id.isin(delRows)]
    id  marks
2  124     67
4  257     10
5  345     34
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.