0

If I have a dataframe as following,

id     price
01      10
02      5
03      0.1
04      100
05      1000

How could I get a new dataframe containing only price greater than $10 (including) and smaller than $100 (including) like this. Thanks.

id     price
01      10
04      100
ah bon
  • 9,293
  • 12
  • 65
  • 148

1 Answers1

3

Use between with boolean indexing:

df = df[df['price'].between(10, 100)]
print (df)
   id  price
0   1   10.0
3   4  100.0

If dont need include values 10 and 100:

df = df[df['price'].between(10, 100, inclusive=False)]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Thanks. what if 10 and 100 not including, how can i deal with it? – ah bon Jun 07 '18 at 06:08
  • 1
    @ahbon df['price'].between(10,100)] just returns True or False for each row depending on whether they fulfill the condition. Then you use this as argument for df[] to pick out the row between 10 and 100. To get the opposite, you just invert the bools, i.e. df[~df['price'].between(10,100)] -- note the ~ – a20 Mar 28 '22 at 12:00