Youtube Video commentary is also available.
P-027: Calculate the average 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.mean().reset_index().sort_values('amount', ascending=False).head(5)
store_cd | amount | |
---|---|---|
28 | S13052 | 402.867470 |
12 | S13015 | 351.111960 |
7 | S13003 | 350.915519 |
30 | S14010 | 348.791262 |
5 | S13001 | 348.470386 |
-Use 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.mean ()'displays the average 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':'mean'}).reset_index().sort_values('amount', ascending=False).head(5)
Recommended Posts