-1

I am trying to set up a script to help to order garment blanks. I have a dataset that looks like this:

|  design  | s | m | l | xl | style   | color |
|----------|---|---|---|----|---------|-------|
| design 1 | 5 | 3 | 6 |  1 | style 1 | black |
| design 2 | 4 | 6 | 9 |  5 | style 2 | red   |
| design 3 | 2 | 6 | 5 |  8 | style 1 | red   |
| design 4 | 6 | 8 | 4 |  1 | style 1 | black |
| design 5 | 8 | 2 | 1 |  1 | style 1 | blue  |
| design 6 | 6 | 9 | 5 |  4 | style 2 | red   |

And I would like to be able to use Pandas to basically sum the totals of each style / color pair so I can order the total amount.

Given the data above, I would like the output to be something like:

| style   | color | s  | m  | l  | xl |
|---------|-------|----|----|----|----|
| style 1 | black | 11 | 11 | 10 | 2  | 
| style 1 | red   | 2  | 6  | 5  | 8  |
| style 1 | blue  | 8  | 2  | 1  | 1  |
| style 2 | red   | 10 | 15 | 14 | 9  |
karel
  • 5,489
  • 46
  • 45
  • 50
Mitchbart
  • 3
  • 2
  • 2
    Ok, pretty standard `groupby` and `agg` work. What went wrong with your attempt? – roganjosh Mar 02 '19 at 20:32
  • 1
    Possible duplicate of [Pandas sum by groupby, but exclude certain columns](https://stackoverflow.com/questions/32751229/pandas-sum-by-groupby-but-exclude-certain-columns) – anky Mar 02 '19 at 20:33

2 Answers2

0
df[['style', 'color', 's','m','l','xl']].groupby(by=['style','color']).sum()

you can add .sort() if you want to sort by items.

Ravi
  • 659
  • 2
  • 10
  • 32
-1
df.groupby("style").cumsum

groupby and cumsum will do what you want.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Arnold Chung
  • 125
  • 2
  • 14