Youtube Video commentary is also available.
P-023: Sum the sales amount (amount) and sales quantity (quantity) for each store code (store_cd) for the receipt detail data frame (df_receipt).
code
df_receipt.groupby('store_cd').agg({'amount':'sum', 'quantity':'sum'}).reset_index()
| store_cd | amount | quantity | |
|---|---|---|---|
| 0 | S12007 | 638761 | 2099 |
| 1 | S12013 | 787513 | 2425 |
| 2 | S12014 | 725167 | 2358 |
| 3 | S12029 | 794741 | 2555 |
| 4 | S12030 | 684402 | 2403 |
| 5 | S13001 | 811936 | 2347 |
| 6 | S13002 | 727821 | 2340 |
| 7 | S13003 | 764294 | 2197 |
| 8 | S13004 | 779373 | 2390 |
| 9 | S13005 | 629876 | 2004 |
| 10 | S13008 | 809288 | 2491 |
| 11 | S13009 | 808870 | 2486 |
| 12 | S13015 | 780873 | 2248 |
| 13 | S13016 | 793773 | 2432 |
| 14 | S13017 | 748221 | 2376 |
| 15 | S13018 | 790535 | 2562 |
| 16 | S13019 | 827833 | 2541 |
| 17 | S13020 | 796383 | 2383 |
| 18 | S13031 | 705968 | 2336 |
| 19 | S13032 | 790501 | 2491 |
| 20 | S13035 | 715869 | 2219 |
| 21 | S13037 | 693087 | 2344 |
| 22 | S13038 | 708884 | 2337 |
| 23 | S13039 | 611888 | 1981 |
| 24 | S13041 | 728266 | 2233 |
| 25 | S13043 | 587895 | 1881 |
| 26 | S13044 | 520764 | 1729 |
| 27 | S13051 | 107452 | 354 |
| 28 | S13052 | 100314 | 250 |
| 29 | S14006 | 712839 | 2284 |
| 30 | S14010 | 790361 | 2290 |
| 31 | S14011 | 805724 | 2434 |
| 32 | S14012 | 720600 | 2412 |
| 33 | S14021 | 699511 | 2231 |
| 34 | S14022 | 651328 | 2047 |
| 35 | S14023 | 727630 | 2258 |
| 36 | S14024 | 736323 | 2417 |
| 37 | S14025 | 755581 | 2394 |
| 38 | S14026 | 824537 | 2503 |
| 39 | S14027 | 714550 | 2303 |
| 40 | S14028 | 786145 | 2458 |
| 41 | S14033 | 725318 | 2282 |
| 42 | S14034 | 653681 | 2024 |
| 43 | S14036 | 203694 | 635 |
| 44 | S14040 | 701858 | 2233 |
| 45 | S14042 | 534689 | 1935 |
| 46 | S14045 | 458484 | 1398 |
| 47 | S14046 | 412646 | 1354 |
| 48 | S14047 | 338329 | 1041 |
| 49 | S14048 | 234276 | 769 |
| 50 | S14049 | 230808 | 788 |
| 51 | S14050 | 167090 | 580 |
** ・ It is a method to process data with the same value collectively in Pandas DataFrame / Series. -Use when you want to check the total or average of data with the same value. -'Groupby' is used when you want to collect data with the same value or character string and perform a common operation (total, average, etc.) for each same value or character string. -'Agg' is an abbreviation for Aggregation (meaning "aggregate"), and is used when you want to perform an operation such as finding a value for each group and creating a table. The sum is'sum', the mean is'mean', the maximum is'max' and the minimum is'min'. -'.Reset_index ()' is used when you want to perform an operation to reassign the index numbers separated by'groupby' to serial numbers starting from 0. ** **
Recommended Posts