Home > Net >  Filter based on min and max
Filter based on min and max

Time:09-30

I have a dataframe as follows

Calls   Weight 
  R       1
  A       1 
  S       3 
  S       3
  Q       7
  W       5
  E       9 

If I have a min of 3 and a max of 5.

I am trying to filter the data so that all values below less than 3 are filtered out. While all values greater than 5 are changed to the max (which is 5)

Expected output:

     Calls   Weight 
      S       3 
      S       3
      Q       5
      W       5
      E       5 

CodePudding user response:

The transformation is straightforward:

df = df[df.Weight >= 3]
df[df.Weight >= 5] = 5

CodePudding user response:

mask = ((df.Weight >= 3) & (df.Weight <= 5))

filtered_df = df[mask]
  • Related