The Python Oracle

Deleting DataFrame row in Pandas based on column value

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Island

--

Chapters
00:00 Deleting Dataframe Row In Pandas Based On Column Value
00:55 Accepted Answer Score 1593
01:06 Answer 2 Score 314
01:28 Answer 3 Score 167
01:45 Answer 4 Score 72
02:01 Thank you

--

Full question
https://stackoverflow.com/questions/1817...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe #performance #deleterow

#avk47



ACCEPTED ANSWER

Score 1593


If I'm understanding correctly, it should be as simple as:

df = df[df.line_race != 0]



ANSWER 2

Score 314


But for any future bypassers you could mention that df = df[df.line_race != 0] doesn't do anything when trying to filter for None/missing values.

Does work:

df = df[df.line_race != 0]

Doesn't do anything:

df = df[df.line_race != None]

Does work:

df = df[df.line_race.notnull()]



ANSWER 3

Score 167


just to add another solution, particularly useful if you are using the new pandas assessors, other solutions will replace the original pandas and lose the assessors

df.drop(df.loc[df['line_race']==0].index, inplace=True)



ANSWER 4

Score 72


If you want to delete rows based on multiple values of the column, you could use:

df[(df.line_race != 0) & (df.line_race != 10)]

To drop all rows with values 0 and 10 for line_race.