Youtube Video commentary is also available.
P-028: Calculate the median sales amount (amount) for each store code (store_cd) for the receipt details data frame (df_receipt), and display the TOP5 in descending order.
code
df_receipt.groupby('store_cd').amount.median().reset_index().sort_values('amount', ascending=False).head(5)
store_cd | amount | |
---|---|---|
28 | S13052 | 190 |
30 | S14010 | 188 |
51 | S14050 | 185 |
44 | S14040 | 180 |
7 | S13003 | 180 |
-Pandas DataFrame / Series. -Use this when you want to process data with the same value together and check the total or average of the data with the same value. -'Groupby' is used when you want to collect data with the same value or character string and perform common operations (total, average, etc.) on each same value or character string. ** ** -'.Amount.median ()' displays the median value of amount. ** ** -'.Reset_index ()'is used when you want to reassign the index numbers that have been separated by'groupby' to serial numbers starting from 0. ** ** -'.Sort_values ('amount', ascending = False)'displays'amount' in descending order. ** **
code
df_receipt.groupby('store_cd').agg({'amount':'median'}).reset_index().sort_values('amount', ascending=False).head(5)
Recommended Posts