Tim Kirkwood
Tim Kirkwood

Reputation: 716

Pandas flexible boolean row sub setting

Is it possible to subset a Pandas 1.3.4 dataframe by multiple row conditions for a single value? To make this clearer, if I have a df:

index    col1    col2    
  0        A      10
  1        A      20
  2        B      130
  3        C      10

If I want to subset based on a col2 value I can do df[df['col2']>10]:

index    col1    col2    
  
  1        A      20
  2        B      130

  

But is it possible to subset col2 using different thresholds based on the col1 value?

For example:

df[df['col2']>10 if col1 == 'A' 
   OR df['col2']>5 if col1 == 'C' 
   OR df['col2']>1000 if col1 == 'B']` 

would give:

index    col1    col2    
  
  1        A      20
  
  3        C      10

Thanks!

Tim

Upvotes: 0

Views: 33

Answers (1)

jezrael
jezrael

Reputation: 863166

If need compare for greater all values use Series.map by dictionary and compare:

d = {'A' : 10,'C': 5 ,'B': 1000}

df[df['col2'] > df['col1'].map(d)]

Or if need different masks chain by & for bitwise AND and | for bitwise OR:

df[((df['col2']>10) & (df['col1']== 'A') ) |
   ((df['col2']>5) & (df['col1']== 'C') ) |
   ((df['col2']>1000) & (df['col1']== 'B') )] 

Upvotes: 4

Related Questions