How to rank and reassign values to subset of Python dataframe
Drawing a blank, please advise. Have a dataframe, d: Position Operation Side Price Size 0 0 0 1 0.7288 -17 1 8 0 1 0.7297 -14 2 7 0 1 0.7296 -8 3 6 0 1 0.7295 -426 4 5 0 1 0.7294 -16 5 4 0 1 0.7293 -16 6 3 0 1 0.7292 -15 7 2 0 1 0.7291 -267 8 1 0 1 0.729 -427 9 0 0 1 0.7289 -16 10 0 0 0 0.7299 6 11 1 0 0 0.73 34 12 2 0 0 0.7301 7 13 3 0 0 0.7302 9 14 4 0 0 0.7303 16 15 5 0 0 0.7304 15 16 6 0 0 0.7305 429 17 7 0 0 0.7306 16 18 8 0 0 0.7307 265 19 9 0 0 0.7308 18 That I'd like to filter based on column value, such as: d.loc[(d.Side==1)] '' Position Operation Side Price Size 0 0 0 1 0.7288 -17 1 8 0 1 0.7297 -14 2 7 0 1 0.7296 -8 3 6 0 1 0.7295 -426 4 5 0 1 0.7294 -16 5 4 0 1 0.7293 -16 6 3 0 1 0.7292 -15 7 2 0 1 0.7291 -267 8 1 0 1 0.729 -427 9 0 0 1 0.7289 -16 I'd then like to rank based on Price, and assign the rank to a column in the subset dataframe, specifically Position. For instance, filtering based on Side = 1, and then ranking based on Price, I'd like my updated dataframe, d, to look like this. Position Operation Side Price Size 0 0 0 1 0.7288 -17 1 9 0 1 0.7297 -14 2 8 0 1 0.7296 -8 3 7 0 1 0.7295 -426 4 6 0 1 0.7294 -16 5 5 0 1 0.7293 -16 6 4 0 1 0.7292 -15 7 3 0 1 0.7291 -267 8 2 0 1 0.729 -427 9 1 0 1 0.7289 -16 10 0 0 0 0.7299 6 11 1 0 0 0.73 34 12 2 0 0 0.7301 7 13 3 0 0 0.7302 9 14 4 0 0 0.7303 16 15 5 0 0 0.7304 15 16 6 0 0 0.7305 429 17 7 0 0 0.7306 16 18 8 0 0 0.7307 265 19 9 0 0 0.7308 18 Where '0' is the lowest value and '9' is the largest--df.rank returns a one-indexed float rank, but it's easy enough to get from there to the int 0-9 values I need. Much appreciated
Drawing a blank, please advise.
Have a dataframe, d
:
Position Operation Side Price Size
0 0 0 1 0.7288 -17
1 8 0 1 0.7297 -14
2 7 0 1 0.7296 -8
3 6 0 1 0.7295 -426
4 5 0 1 0.7294 -16
5 4 0 1 0.7293 -16
6 3 0 1 0.7292 -15
7 2 0 1 0.7291 -267
8 1 0 1 0.729 -427
9 0 0 1 0.7289 -16
10 0 0 0 0.7299 6
11 1 0 0 0.73 34
12 2 0 0 0.7301 7
13 3 0 0 0.7302 9
14 4 0 0 0.7303 16
15 5 0 0 0.7304 15
16 6 0 0 0.7305 429
17 7 0 0 0.7306 16
18 8 0 0 0.7307 265
19 9 0 0 0.7308 18
That I'd like to filter based on column value, such as:
d.loc[(d.Side==1)]
''
Position Operation Side Price Size
0 0 0 1 0.7288 -17
1 8 0 1 0.7297 -14
2 7 0 1 0.7296 -8
3 6 0 1 0.7295 -426
4 5 0 1 0.7294 -16
5 4 0 1 0.7293 -16
6 3 0 1 0.7292 -15
7 2 0 1 0.7291 -267
8 1 0 1 0.729 -427
9 0 0 1 0.7289 -16
I'd then like to rank
based on Price
, and assign the rank to a column in the subset dataframe, specifically Position
.
For instance, filtering based on Side
= 1, and then ranking based on Price
, I'd like my updated dataframe, d
, to look like this.
Position Operation Side Price Size
0 0 0 1 0.7288 -17
1 9 0 1 0.7297 -14
2 8 0 1 0.7296 -8
3 7 0 1 0.7295 -426
4 6 0 1 0.7294 -16
5 5 0 1 0.7293 -16
6 4 0 1 0.7292 -15
7 3 0 1 0.7291 -267
8 2 0 1 0.729 -427
9 1 0 1 0.7289 -16
10 0 0 0 0.7299 6
11 1 0 0 0.73 34
12 2 0 0 0.7301 7
13 3 0 0 0.7302 9
14 4 0 0 0.7303 16
15 5 0 0 0.7304 15
16 6 0 0 0.7305 429
17 7 0 0 0.7306 16
18 8 0 0 0.7307 265
19 9 0 0 0.7308 18
Where '0' is the lowest value and '9' is the largest--df.rank
returns a one-indexed float rank, but it's easy enough to get from there to the int 0-9 values I need.
Much appreciated