Danish
Danish

Reputation: 2871

Filter data frame based on percentile range of one column in pandas

I have a data frame as shown below

ID     Price
1      5
2      90
3      20
4      40
5      30
6      70
7      60
8      100
9      80
10     50

From the above I would like to filter above data frame from 10 percentile to 90 percentile as shown below.

Expected output:

ID     Price
2      90
3      20
4      40
5      30
6      70
7      60
9      80
10     50

Upvotes: 0

Views: 190

Answers (1)

ALollz
ALollz

Reputation: 59519

Use between + Series.quantile

df[df['Price'].between(*df['Price'].quantile([0.1, 0.9]))]

   ID  Price
1   2     90
2   3     20
3   4     40
4   5     30
5   6     70
6   7     60
8   9     80
9  10     50

Upvotes: 2

Related Questions