1

I have a table like this

AREA     AMOUNT
A         1000
A          10
B          30
B         3000
C          22
D         300

What I want to get is more that 100 in AREA A & more than 100 in AREA B & less than 100 in AREA C and more than 100 in AREA D . I have many of these kind of area to analyse. so what I want to get is below.

AREA     AMOUNT
A         1000
B         3000
C          22
D         300 
Z.L
  • 147
  • 8

2 Answers2

4

You can use .isin() and pass the three columns > 100 and then == for just the C column using & and | for and and or. Pay attention to parentheses here:

df = df[((df['AREA'].isin(['A','B','D'])) & (df['AMOUNT'] > 100)) |
        ((df['AREA'] == 'C')              & (df['AMOUNT'] < 100))]
df
Out[1]: 
  AREA  AMOUNT
0    A    1000
3    B    3000
4    C      22
5    D     300
David Erickson
  • 16,433
  • 2
  • 19
  • 35
  • Thank you it's a fast way to solve my problem and can add new condition easily in this solution – Z.L Dec 05 '20 at 08:27
0

You can write in this way also by creating custom function for setting up the condition

import operator
ops = {'eq': operator.eq, 'neq': operator.ne, 'gt': operator.gt, 'ge': operator.ge, 'lt': operator.lt, 'le': operator.le}
g = lambda x, y, z: (df['AREA'].eq(x)) & (ops[z](df['AMOUNT'], y))

df[g('A', 100, 'gt')| g('B', 100, 'gt') | g('C', 100, 'lt') | g('D', 100, 'gt') ]

   AREA AMOUNT
0   A   1000
3   B   3000
4   C   22
5   D   300
Pygirl
  • 12,969
  • 5
  • 30
  • 43