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
Piyush S. Wanare
  • 4,703
  • 6
  • 37
  • 54
  • Possible duplicate of [How to drop rows from pandas data frame that contains a particular string in a particular column?](https://stackoverflow.com/questions/28679930/how-to-drop-rows-from-pandas-data-frame-that-contains-a-particular-string-in-a-p) – m.rp Jul 04 '18 at 07:31

1 Answers1

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252