2

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

ok, thank you. I just tried the first suggestion and still have the same number of rows. Let me try the latter- Does it matter that I have multiple columns?
It wouldn't matter if you have more columns. It does matter, if you have other stuff in the cell. For example 'hi ' cannot be detected with isin(['hi','no']).
ok @quang, does it matter if my datatype is showing Object and not string?
I will accept because it works with my sample dataset, but it is not working with my full dataset for some reason. I have only 3 columns in the full dataset. The values I wish to remove resemble this: SJ4, IA9 etc - not sure why it would not work
@Lynnette like I said 'hi ', with a trailing space, cannot be detected with isin, which finds the exact value. str.contains searches for a substring and would work.
|

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.