Lynn
Lynn

Reputation: 4398

Delete rows based on multiple conditions within a column (using Python)

I have a dataset, df, where I wish to remove the rows from if the column contains a certain value(s)

  value   pod

  1       hi
  2       ok
  3       no
  4       sure

Desired output:

  value   pod

  2       ok
  4       sure

I wish to remove the row if the pod column contains the word 'hi' or the word 'no'

This is what I am doing

df1 = df.drop(df.index[df['pod'] == 'hi', 'no'], inplace = True)

I keep getting this error:

A value is trying to be set on a copy of a slice from a DataFrame

I am still researching this, any suggestion is appreciated

Upvotes: 2

Views: 1107

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

I believe you should use isin:

df1 = df[~df['pod'].isin(['hi','no']) ]

print(df1)

Output:

   value   pod
1      2    ok
3      4  sure

On the other note, look at the following:

df.drop(df.index[df['pod'] == 'hi', 'no'], inplace = True)

if it were to work, inplace=True forces the drop command to work inplace, and return None. So

df1 = df.drop(df.index[df['pod'] == 'hi', 'no'], inplace = True)

would mean that df1 is None, not a dataframe.

Upvotes: 2

Related Questions