Home > Mobile >  Filter dataframe based on values from 2 columns
Filter dataframe based on values from 2 columns

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

I want to filter the dataframe in such a way that values from BG 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

CodePudding user response:

Use:

>>> df[(df["country"].eq("BG")&df["values"].abs().lt(5))|(df["country"].ne("BG")&df["values"].lt(5))]

  country  values
1      BG       4
2      BG       3
3      BG      -3
6      DE       3
7      DE     -20
9     IND      -2
  • Related