Youtube Video commentary is also available.
P-020: Give ranks to the receipt details data frame (df_receipt) in descending order of sales amount (amount) per item, and extract the first 10 items. Items should display customer ID (customer_id), sales amount (amount), and assigned rank. In addition, even if the sales amount (amount) is the same, give a different ranking.
code
df_amount_rank = pd.concat([df_receipt[['customer_id', 'amount']] \
,df_receipt['amount'].rank(method='first', ascending=False)], axis=1)
df_amount_rank.columns = ['customer_id', 'amount', 'amount_ranking']
df_amount_rank.sort_values('amount_ranking', ascending=True).head(10)
customer_id | amount | amount_ranking | |
---|---|---|---|
1202 | CS011415000006 | 10925 | 1 |
62317 | ZZ000000000000 | 6800 | 2 |
54095 | CS028605000002 | 5780 | 3 |
4632 | CS015515000034 | 5480 | 4 |
10320 | ZZ000000000000 | 5480 | 5 |
72747 | ZZ000000000000 | 5480 | 6 |
28304 | ZZ000000000000 | 5440 | 7 |
97294 | CS021515000089 | 5440 | 8 |
596 | CS015515000083 | 5280 | 9 |
11275 | CS017414000114 | 5280 | 10 |
**-In Pandas DataFrame / Series, create a new rank column, concatenate the columns, and rank the data.
-Use when you want to see numerical information in ranking format.
-'Concat ('
Recommended Posts