-4

I've had a good hunt for some time and can't find a solution so asking here.

I have data like so:

Plan         | Quantity_y
Starter      | 1
Intermediate | 1
Intermediate | 1
Intermediate | 2
Intermediate | 1
Intermediate | 14
Intermediate | 1
Advanced     | 1
Advanced     | 1
Advanced     | 2
Advanced     | 1
Incredible   | 1
Incredible   | 1
Incredible   | 1
Incredible   | 1
Incredible   | 1
Incredible   | 2
Incredible   | 2

and I'd like it to group AND count the individual numbers like so:

Plan         | Quantity_y
Starter      | 1
Intermediate | 20
Advanced     | 5
Incredible   | 9

I've tried so many options but am having no luck at all.

I've tried doing an iterrows() as well as trying to assign a value by counting and returning the count via a .apply() function call. I'm just not getting how I can group AND count the sum of the numbers in that group.

Any advice would be appreciated.

Thank you

robster
  • 626
  • 1
  • 7
  • 22

1 Answers1

-4
df.groupby('Plan', sort=False).sum()
Panda Kim
  • 6,246
  • 2
  • 12
  • 2
    What the point of answering this many times duplicate? – mozway Dec 21 '22 at 06:25
  • This doesn't have the result I'm expecting in my original question. Thank you for trying though. This doesn't individually count the second column (Quantity_y) and group by the first (Plan). – robster Dec 21 '22 at 06:33
  • I found it, it's the `.agg()` function. Will add answer once the time has passed enough to allow me to. – robster Dec 21 '22 at 06:36
  • 2
    only sum don need `agg` . Do you see comment about why i posted answer not make duplicated? It rather disproves that code is correct. maybe your dataset is differ from example. your dataset have many columns different from example. then use following : `df.groupby('Plan', sort=False)['Quantity_y'].sum()` – Panda Kim Dec 21 '22 at 06:43