resolver101
resolver101

Reputation: 2255

pandas filtering: selecting multiple column if either statement is true

I have p_order_items data frame that looks like below:

JOBNO   JOBNO2  JOBNO4  JOBNO5  JOBNO6  
50626   S14868  NaN      NaN    NaN 
28196   S12839  NaN      22222  NaN 
23907   S12392  NaN      NaN    222222  
38833   S14050  S14051   NaN    NaN

I'm trying to pull out rows where there is a number in either of the columns JOBNO2, JOBNO4, JOBNO5, JOBNO6?

I can see how to do it when all are true using "&" but not if either is true "or":

p_order_items[p_order_items.JOBNO2.notnull() &                                                 
              p_order_items.JOBNO4.notnull() &                                                 
              p_order_items.JOBNO5.notnull() &                                                 
              p_order_items.JOBNO6.notnull()]

Can anyone tell me how to get all the rows that have a number/value in any of the columns?

Thanks.

Upvotes: 0

Views: 47

Answers (2)

Jaroslav Bezděk
Jaroslav Bezděk

Reputation: 7625

Try this:

p_order_items[(p_order_items.JOBNO2.notnull()) | (p_order_items.JOBNO4.notnull()) | (p_order_items.JOBNO5.notnull()) | (p_order_items.JOBNO6.notnull())] 

In pandas, "&" stands for and, pipe ("|") stands for or.

Upvotes: 1

Georgina Skibinski
Georgina Skibinski

Reputation: 13387

Just replace "&" with "|":

p_order_items[p_order_items.JOBNO2.notnull() | p_order_items.JOBNO4.notnull() | p_order_items.JOBNO5.notnull() | p_order_items.JOBNO6.notnull()]

Upvotes: 1

Related Questions