Home >Backend Development >Python Tutorial >How to Calculate Total Fruit Purchases by Name Using Pandas GroupBy?

How to Calculate Total Fruit Purchases by Name Using Pandas GroupBy?

Barbara Streisand
Barbara StreisandOriginal
2024-12-26 00:20:11965browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn