0

I have a dataFrame like this: enter image description here

I wonder how to drop the whole row if any specific columns contain a specific value?

For example, If columns Q1, Q2 or Q3 contain zero, delete the whole row. But if columns Q4 or Q5 contain zero, do not delete the row.

enter image description here

Brown Bear
  • 19,655
  • 10
  • 58
  • 76
Wenjing Li
  • 105
  • 6
  • Welcome on SO please, read two points [why-not-upload-images-of-code](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) and [how-to-make-good-reproducible-pandas-examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Brown Bear Jul 01 '20 at 09:14

2 Answers2

1

Use loc to filter with eq and any along axis 1, and logical NOT operator ~:

df.loc[~df[['Q1', 'Q2', 'Q3']].eq(0).any(1)]

Example

import pandas as pd
import numpy as np

np.random.seed(0)
df = pd.DataFrame(np.random.randn(5,5), columns=['Q1', 'Q2', 'Q3', 'Q4', 'Q5'])

df.loc[1,'Q1'] = 0
df.loc[4, 'Q2'] = 0
df.loc[3, 'Q5'] = 0

[out]

         Q1        Q2        Q3        Q4        Q5
0  1.764052  0.400157  0.978738  2.240893  1.867558
1  0.000000  0.950088 -0.151357 -0.103219  0.410599
2  0.144044  1.454274  0.761038  0.121675  0.443863
3  0.333674  1.494079 -0.205158  0.313068  0.000000
4 -2.552990  0.000000  0.864436 -0.742165  2.269755

# Should drop rows 1 and 4, but leave row 3

df.loc[~df[['Q1', 'Q2', 'Q3']].eq(0).any(1)]

[out]

         Q1        Q2        Q3        Q4        Q5
0  1.764052  0.400157  0.978738  2.240893  1.867558
2  0.144044  1.454274  0.761038  0.121675  0.443863
3  0.333674  1.494079 -0.205158  0.313068  0.000000
Chris Adams
  • 18,389
  • 4
  • 22
  • 39
0

We can consider this a conditional filtering problem. We want to only keep rows, where the columns Q1, Q2 and Q3 are non-zero:

df_new = df[(df["Q1"] != 0) & (df["Q2"] != 0) & (df["Q3"] != 0)]

This df_new now only contains rows that you want.

mabergerx
  • 1,216
  • 7
  • 19