


How to Calculate Total Fruit Purchases by Name Using Pandas GroupBy?
Calculating Fruit Totals by Name using Pandas Group-By Sum
Grouping and aggregation are essential operations when working with data. Pandas provides a powerful GroupBy function that simplifies these processes.
Consider the following DataFrame where you want to calculate the total number of fruits purchased by each Name:
Fruit Date Name Number Apples 10/6/2016 Bob 7 Apples 10/6/2016 Bob 8 Apples 10/6/2016 Mike 9 Apples 10/7/2016 Steve 10 Apples 10/7/2016 Bob 1 Oranges 10/7/2016 Bob 2 Oranges 10/6/2016 Tom 15 Oranges 10/6/2016 Mike 57 Oranges 10/6/2016 Bob 65 Oranges 10/7/2016 Tony 1 Grapes 10/7/2016 Bob 1 Grapes 10/7/2016 Tom 87 Grapes 10/7/2016 Bob 22 Grapes 10/7/2016 Bob 12 Grapes 10/7/2016 Tony 15
To achieve this, we can use the GroupBy function to group the DataFrame by both "Name" and "Fruit":
df.groupby(['Name', 'Fruit'])
However, this only groups the data without performing any aggregations. To calculate the sum of "Number" for each group, we can use sum():
df.groupby(['Name', 'Fruit']).sum()
This will output a new DataFrame with a hierarchical index, where the first level corresponds to "Name" and the second level corresponds to "Fruit". The "Number" column contains the sum for each group:
Number Name Fruit Bob Apples 16 Grapes 35 Oranges 67 Mike Apples 9 Oranges 57 Steve Apples 10 Tom Grapes 87 Oranges 15 Tony Grapes 15 Oranges 1
This gives us the desired result, showing the total number of fruits purchased by each Name.
The above is the detailed content of How to Calculate Total Fruit Purchases by Name Using Pandas GroupBy?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Using python in Linux terminal...
