Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select rows which have only zeros in columns

I want to select the rows in a dataframe which have zero in every column in a list of columns. e.g. this df:.

In: 
    df = pd.DataFrame([[1,2,3,6], [2,4,6,8], [0,0,3,4],[1,0,3,4],[0,0,0,0]],columns =['a','b','c','d'])
    df

Out:
    a   b   c   d
0   1   2   3   6
1   2   4   6   8
2   0   0   3   4
3   1   0   3   4
4   0   0   0   0

Then:

In:
    mylist = ['a','b']
    selection = df.loc[df['mylist']==0]
    selection

I would like to see:

Out:      
    a   b   c   d
2   0   0   3   4
4   0   0   0   0

Should be simple but I'm having a slow day!

like image 496
doctorer Avatar asked Oct 18 '25 10:10

doctorer


2 Answers

You'll need to determine whether all columns of a row have zeros or not. Given a boolean mask, use DataFrame.all(axis=1) to do that.

df[df[mylist].eq(0).all(1)]

   a  b  c  d
2  0  0  3  4
4  0  0  0  0

Note that if you wanted to find rows with zeros in every column, remove the subsetting step:

df[df.eq(0).all(1)]

   a  b  c  d
4  0  0  0  0
like image 87
cs95 Avatar answered Oct 21 '25 00:10

cs95


Using reduce and Numpy's logical_and
The point of this is to eliminate the need to create new Pandas objects and simply produce the mask we are looking for using the data where it sits.

from functools import reduce

df[reduce(np.logical_and, (df[c].values == 0 for c in mylist))]

   a  b  c  d
2  0  0  3  4
4  0  0  0  0
like image 37
piRSquared Avatar answered Oct 20 '25 23:10

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!