简体   繁体   中英

Pandas: Find rows where last five columns satisfy condition

I have a DataFrame, df , that has 100 columns. The last 5 of those columns take on binary values. I want to find all rows where the the last five columns look like: 0, 1, 0, 1, 1.

I could do this by and ing conditions like:

df[(df.col95 == 0) & (df.col96 == 1) ...] but I'm sure there's a better way and I don't want to name the columns.

df[df.apply(lambda row: tuple(row[-5:]) == (0, 1, 0, 1, 1))]

But I get an Unalignable Boolean series provided as indexer error. Any suggestions for how to accomplish this?

Thanks!

您可以在此处使用merge

df.merge(pd.DataFrame([[0,1,0,1,1]],columns=df.columns[-5:]))

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