@@ -407,39 +407,49 @@ Let's add a helper column: `RN` (Row Number)
407
407
408
408
.. ipython :: python
409
409
410
- tips[' rn' ] = (tips.sort_values([' total_bill' ], ascending = False )
411
- .groupby([' day' ])
412
- .cumcount() + 1
413
- )
414
- tips.loc[tips[' rn' ] < 3 ].sort_values([' day' ,' rn' ])
410
+ (tips.assign(rn = tips.sort_values([' total_bill' ], ascending = False )
411
+ .groupby([' day' ])
412
+ .cumcount() + 1 )
413
+ .query(' rn < 3' )
414
+ .sort_values([' day' ,' rn' ])
415
+ )
415
416
416
417
the same using `rank(method='first') ` function
417
418
418
419
.. ipython :: python
419
420
420
- tips[' rnk' ] = (tips.groupby([' day' ])[' total_bill' ]
421
- .rank(method = ' first' , ascending = False )
422
- )
423
- tips.loc[tips[' rnk' ] < 3 ].sort_values([' day' ,' rnk' ])
421
+ (tips.assign(rnk = tips.groupby([' day' ])[' total_bill' ]
422
+ .rank(method = ' first' , ascending = False ))
423
+ .query(' rnk < 3' )
424
+ .sort_values([' day' ,' rnk' ])
425
+ )
424
426
425
427
.. code-block :: sql
426
428
427
429
-- Oracle's RANK() analytic function
428
430
SELECT * FROM (
429
431
SELECT
430
432
t.*,
431
- RANK() OVER(PARTITION BY day ORDER BY total_bill DESC ) AS rnk
433
+ RANK() OVER(PARTITION BY sex ORDER BY tip ) AS rnk
432
434
FROM tips t
435
+ WHERE tip < 2
433
436
)
434
437
WHERE rnk < 3
435
- ORDER BY day, rn;
438
+ ORDER BY sex, rnk;
439
+
440
+ Let's find tips with (rank < 3) per gender group for (tips < 2).
441
+ Notice that when using ``rank(method='min') `` function
442
+ `rnk_min ` remains the same for the same `tip `
443
+ (as Oracle's RANK() function)
436
444
437
445
.. ipython :: python
438
446
439
- tips[' rnk_min' ] = (tips.groupby([' day' ])[' total_bill' ]
440
- .rank(method = ' min' , ascending = False )
441
- )
442
- tips.loc[tips[' rnk_min' ] < 3 ].sort_values([' day' ,' rnk_min' ])
447
+ (tips[tips[' tip' ] < 2 ]
448
+ .assign(rnk_min = tips.groupby([' sex' ])[' tip' ]
449
+ .rank(method = ' min' ))
450
+ .query(' rnk_min < 3' )
451
+ .sort_values([' sex' ,' rnk_min' ])
452
+ )
443
453
444
454
445
455
UPDATE
0 commit comments