简体   繁体   中英

dataframe.isin using multiple OR operators

I want to see if I have some values from a list in multiple columns from my pandas dataframe.

Basically, I want to look over my column A, B and C if the values from a list exists and if so filter the rows where those values exist. For that I am using this:

processes = ['process_A', 'process_B']
df[df.col_A.isin(processes),df.col_B.isin(processes), df.col_C.isin(processes)].any()

And already try this:

df[df.col_A.isin(processes) OR df.col_B.isin(processes) or df.col_C.isin(processes)]

But I got a lot of errors or unexpectables results.

.isin returns a boolean Series, you need to use | for logical OR between the values.

try this:

df[df.col_A.isin(processes) | df.col_B.isin(processes) | df.col_C.isin(processes)]

使用any如下

df[df[['col_A','col_B','col_C']].isin(processes).any(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