Home > other >  Filter dataframe based on 2 columns with values
Filter dataframe based on 2 columns with values

Time:04-08

I have a dataframe

country values
BG 20
BG 4
BG 3
BG -3
BG -20
DE 20
DE 3
DE -20
IND 20
IND -2
GB 30
GB 3
GB -30

I want to filter the dataframe in such a way that values from BG and GB should be < absolute (5) and all other countries should be < 5 such that the dataframe becomes

country values
BG 4
BG 3
BG -3
DE 3
DE -20
IND -2
GB 3

CodePudding user response:

You could use a mask:

mask = (df['values'].mask(df['country'].isin(['BG', 'GB']), # if BG/GB
                          df['values'].abs())               # get abs
                    .lt(5)                                  # now is it < 5?
        )
df[mask]

output:

   country  values
1       BG       4
2       BG       3
3       BG      -3
6       DE       3
7       DE     -20
9      IND      -2
11      GB       3

alternative: using explicit conditions

# is country GB or GB
m1 = df['country'].isin(['BG', 'GB'])
# is abs(value) < 5
m2 = df['values'].abs().lt(5)
# is value < 5
m3 = df['values'].lt(5)

df[(m1&m2)|(~m1&m3)]   # for GB/BG get mask m2, else get mask m3

CodePudding user response:

Use:

df[((df['country'].isin(['BG', 'GB']))&(df['values'].abs()<5))|(df['values']<5)]
  • Related