How can I group and sum a pandas dataframe? [duplicate]

2024/11/8 23:37:24

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

Answer
df.groupby('Plan', sort=False).sum()
https://en.xdnf.cn/q/120558.html

Related Q&A

python add value to a list when iterate the list

values = [2,3,4] for v in values:values.append([v,255,255])Why do the statements above never end? I make a mistake in my code. However, I find it will never stop when I execute the code above.

resizing images from 64x64 to 224x224 for the VGG model

Can we resize an image from 64x64 to 256x256 without affecting the resolution is that a way to add zero on new row and column in the new resized output I m working on vgg and I get an error while addin…

Python slicing explained [duplicate]

This question already has answers here:How slicing in Python works(38 answers)Closed 6 years ago.OK I understand the basics, but can someone explain code copied from Gregs answer here:a[1::-1] # the …

Comparison between string characters within a list [duplicate]

This question already has an answer here:How to compare characters of strings that are elements of a list? [duplicate](1 answer)Closed 2 years ago.Having a Python list, containing same length strings,…

Pyspark filling missing dates by group and filling previous values

Spark version 3.0. I have two dataframes. I create one dataframe with date columns using pandas date range. I have a 2nd spark dataframe contains the company name, dates and value. I want to merge the …

How to loop in the opposite order?

I am a beginner programmer. Here is my code:n = int(input()) from math import* for i in range(n):print(n, "\t", log10(n))i = i + 1n = n - 1Its output is:10 1.0 9 0.9542425094393249 8 …

Override methods with same name in Python programming [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate: How do I use method overloading in Python?I am new to Python programming, and I like to write multiple methods with the …

TypeError: function object is not subscriptable in Python 3.4.3?

I have a food menu and the stock and prices are in separate dictionaries.Food Stock:Food_Stock = {Chips : 15,Bagels : 27,Cookies : 25}#Food Stock.Food Prices:Food_Prices = {#Food Prices.Chips : 1,Bagel…

Insert a value in date format dd-mm-yyyy in dictionary in python

I am creating a dictionary having some values including a date of birth of a person. But when I run my code, it is giving an error "datetime.datetime has no attribute datetime" . Here is my …

Collisions arent registering with Python Turtles

Im creating a simple 2D shooter following an online tutorial, and the enemy sprites (except 1, there are 5 total) are not abiding by my collision code. Everything works except the bullet object that th…