简体   繁体   中英

Drop based on multiple str.contains

I'm trying to drop all rows except where one of two conditions is met in a column, ideally I can do this with a single line of code

DF:

Col1 | Col2  | 

Foo    Bar     
Foo    Bar     
Foo    Too     
Foo    apple   

I tried:

DF = DF[~((DF['Col1'].astype(str).str.contains('Foo')) & (DF['Col2'].astype(str) != ('Bar')) &  (DF['Col2'].astype(str) != ('Too'))]

However, it's deleting all rows does the '&' replace an OR?

The expected result is:

Col1 | Col2  | 

Foo    Bar     
Foo    Bar     
Foo    Too      

For a logical bitwise AND you should be using & . And for the second column you can simplify using isin :

df[df.Col1.str.contains('Foo') & df.Col2.isin(['Bar', 'Too'])]

   Col1   Col2
0  Foo    Bar
1  Foo    Bar
2  Foo    Too

Now following what you mention your expected output is, you should use the above. If what you do want is the rows where any conditions are fulfilled, you should be using | .

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