Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

excluding rows from a pandas dataframe based on column value and not index value

Tags:

python

pandas

I looked at the unique values in a column of a dataframe - pandas that I have. And there are some names in one of the columns that I do not want to include, how do I remove those rows from the dataframe, without using index value notation, but by saying if row value = "this" then remove

like...

new = df.copy

df['some column'].drop_values('this','that','other')
like image 414
yoshiserry Avatar asked Mar 13 '14 22:03

yoshiserry


People also ask

How do you exclude rows from a DataFrame in Python?

drop() method you can drop/remove/delete rows from DataFrame. axis param is used to specify what axis you would like to remove. By default axis = 0 meaning to remove rows. Use axis=1 or columns param to remove columns.

How do you drop pandas rows based on condition?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).


1 Answers

See indexing with isin (also, boolean indexing):

mask = df['some column'].isin(['this', 'that', 'other'])
df[~mask]
like image 128
behzad.nouri Avatar answered Oct 02 '22 17:10

behzad.nouri