简体   繁体   中英

Subsetting pandas dataframe for identical value on several consecutive columns

I have a pandas data frame of the following type;

id | a | b | c | d | e | g
---------------------------

1  | 0 | 1 | 0 | 1 | 1  | 1

2  | 0 | 0 | 0 | 0 | 1  | 0

3  | 0 | 0 | 0 | 0 | 1  | 1

I want to filter rows which has zero for all columns, a through d . I am aware that this can be achieved by;

data[data['a']==0 & data['b']==0 & data ['c'] ==0 & data ['d'] ==0]

But is there a quicker way using iloc or loc to achieve the same. For example, I tried the following;

data.iloc[:,[1:4]==0]

But it gave me following error message; ValueError: Can only index by location with a [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array]

Any alternate feedback/advice, which can replace my original solution is appreciated.thanks

Do you mean:

df[df.iloc[:,1:4].eq(0).all(1)]

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