简体   繁体   中英

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. eg 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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM