doctorer
doctorer

Reputation: 1772

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!

Upvotes: 4

Views: 11274

Answers (2)

piRSquared
piRSquared

Reputation: 294258

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

Upvotes: 1

cs95
cs95

Reputation: 402483

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

Upvotes: 10

Related Questions