2

I want to delete all rows containing required string ,

Suppose I have following dataframe:

A    B    C
1    a    x
w    g    n
3    l    p
j    p    v

I want to delete all rows containing string p. I have search for it but most of the answer is on the basis of column name , in my case I will not be aware of column it can be present in any of the column.

Output dataframe should be

A    B    C
1    a    x
w    g    n
1

1 Answer 1

7

For filtering strings:

df = df[(df != 'p').all(axis=1)]

Compare for not equal:

print ((df != 'p'))
      A      B      C
0  True   True   True
1  True   True   True
2  True   True  False
3  True  False   True

And test for all Trues per row:

print ((df != 'p').all(axis=1))
0     True
1     True
2    False
3    False
dtype: bool

Or:

df = df[~(df == 'p').any(axis=1)]

Test for equal:

print ((df == 'p'))
       A      B      C
0  False  False  False
1  False  False  False
2  False  False   True
3  False   True  False

Test at least one True per row:

print ((df == 'p').any(axis=1))
0    False
1    False
2     True
3     True
dtype: bool

Invert boolean mask:

print (~(df == 'p').any(axis=1))
0     True
1     True
2    False
3    False
dtype: bool

For filtering substrings use contains with apply:

df = df[~df.apply(lambda x: x.astype(str).str.contains('p')).any(axis=1)]

Or:

df = df[~df.stack().astype(str).str.contains('p').unstack().any(axis=1)]

print (df)
   A  B  C
0  1  a  x
1  w  g  n
Sign up to request clarification or add additional context in comments.

4 Comments

Ca I pass list of values to it? Or need to do using for loop only.
@PiyushS.Wanare - Yes, then use df = df[~(df.isin(['p','a'])).any(1)]
@PiyushS.Wanare - And for subtrings df = df[~df.apply(lambda x: x.astype(str).str.contains('p|a')).any(axis=1)]
Thanks brother !!

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.