Home >Backend Development >Python Tutorial >How Can Pandas GroupBy Calculate Total Fruits per Name?

How Can Pandas GroupBy Calculate Total Fruits per Name?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-23 06:02:19191browse

How Can Pandas GroupBy Calculate Total Fruits per Name?

Pandas Group-By: Determining Fruit Totals by Name

In data manipulation scenarios, the need often arises to aggregate data based on specified criteria. Pandas' groupby method offers a powerful solution for grouping data and performing various aggregations. This article will demonstrate how to use groupby to calculate the sum of fruits for each name in a given dataset.

Problem Statement:

Given a dataframe with columns representing Fruit, Date, Name, and Number:

import pandas as pd

df = pd.DataFrame({
    'Fruit': ['Apples', 'Apples', 'Apples', 'Apples', 'Apples', 'Oranges', 'Oranges', 'Oranges', 'Oranges', 'Oranges', 'Grapes', 'Grapes', 'Grapes', 'Grapes', 'Grapes'],
    'Date': ['10/6/2016', '10/6/2016', '10/6/2016', '10/7/2016', '10/7/2016', '10/7/2016', '10/6/2016', '10/6/2016', '10/6/2016', '10/7/2016', '10/7/2016', '10/7/2016', '10/7/2016', '10/7/2016', '10/7/2016'],
    'Name': ['Bob', 'Bob', 'Mike', 'Steve', 'Bob', 'Bob', 'Tom', 'Mike', 'Bob', 'Tony', 'Bob', 'Tom', 'Bob', 'Bob', 'Tony'],
    'Number': [7, 8, 9, 10, 1, 2, 15, 57, 65, 1, 1, 87, 22, 12, 15]
})

Solution:

To calculate the sum of fruits for each name, follow these steps:

result_df = df.groupby(['Fruit', 'Name'])['Number'].sum()

This operation will group the data by both 'Fruit' and 'Name' columns and aggregate the 'Number' column by taking the sum. The output will be a dataframe containing the total number of fruits for each combination of 'Fruit' and 'Name'.

Result:

The resulted dataframe will resemble the following:

               Number
Fruit   Name         
Apples  Bob        16
        Mike        9
        Steve      10
Grapes  Bob        35
        Tom        87
        Tony       15
Oranges Bob        67
        Mike       57
        Tom        15
        Tony        1

If a specific column needs to be specified for aggregation, the following syntax can be used:

result_df = df.groupby(['Name', 'Fruit'])['Number'].sum()

By using Pandas' groupby and sum functions, we can effectively group and aggregate data to obtain meaningful insights from large datasets, making it a powerful tool for data exploration and analysis.

The above is the detailed content of How Can Pandas GroupBy Calculate Total Fruits per Name?. 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