0

I have a df with multiple columns and trying to select a subset of the data based on an OR logic:

df [ (df['col1']==0) | (df['col2']==0) | (df['col3']==0) | (df['col4']==0) |
(df['col5']==0) | (df['col6']==0) | (df['col7']==0) | (df['col8']==0) |
(df['col9']==0) | (df['col10']==0) | (df['col11']==0) ]

When I apply this logic the result is empty but I know some of the values are zero

All the values of the these column are int64.

I noticed that 'col11' are all 1's. When I remove 'col11' or swap the order of the query (e.g., putting "| (df['col11']==0)" in the middle )I get the expected results.

I wonder if anyone has had this problem or any ideas what's the reason I'm returning an empty df.

gemini35
  • 3
  • 1
  • There's nothing wrong with that code as written. It would be awfully easy to commit a typo with that. Can you show us a complete runnable example? – Tim Roberts Jun 27 '22 at 17:08
  • [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Jun 27 '22 at 17:10

1 Answers1

0

Use (df==0).any(axis=1)

df...

    a   b   c   d   e   f
0   6   8   7  19   3  14
1  14  19   3  13  10  10
2   6  18  16   0  15  12
3  19   4  14   3   8   3
4   4  14  15   1   6  11

>>> (df==0).any(axis=1)
0    False
1    False
2     True
3    False
4    False
>>> #subset of the columns
>>> (df[['a','c','e']]==0).any(axis=1)
0    False
1    False
2    False
3    False
4    False
dtype: bool

If the DataFrame is all integers you can make use of the fact that zero is falsey and use

~df.all(axis=1)

To make fake data

import numpy as np
import pandas as pd
rng = np.random.default_rng()
nrows = 5
df = pd.DataFrame(rng.integers(0,20,(nrows,6)),columns=['a', 'b', 'c', 'd','e','f'])
wwii
  • 23,232
  • 7
  • 37
  • 77
  • This works for me! It's much cleaner, too. Not sure still having problems using the long way. I wonder if maybe if something got corrupted during data cleaning process (deleting, renaming, and merging). – gemini35 Jun 27 '22 at 18:18