maggs
maggs

Reputation: 803

Delete Rows in pandas dataframe

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???

Upvotes: 1

Views: 360

Answers (1)

jezrael
jezrael

Reputation: 862481

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

Upvotes: 1

Related Questions