JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

How to get a Pandas sum using group-by and sum

Author:JIYIK Last Updated:2025/05/02 Views:

We will demonstrate how to get the sum of Pandas groupby and sum. We will also look at pivotthe function to arrange the data in a nice table and how to define custom functions and apply them to DataFrame. We can also get the sum by using agg().


groupbyThe cumulative sum of

We can use groupbythe method to get the cumulative sum. For example, the following has date, fruit name and sales DataFrame:

import pandas as pd

df = pd.DataFrame(
    {
        "Date": ["08/09/2018", "10/09/2018", "08/09/2018", "10/09/2018"],
        "Fruit": [
            "Apple",
            "Apple",
            "Banana",
            "Banana",
        ],
        "Sale": [34, 12, 22, 27],
    }
)

If we want to calculate the cumulative sales total for each fruit on each date, we can do the following:

import pandas as pd

df = pd.DataFrame(
    {
        "Date": ["08/09/2018", "10/09/2018", "08/09/2018", "10/09/2018"],
        "Fruit": [
            "Apple",
            "Apple",
            "Banana",
            "Banana",
        ],
        "Sale": [34, 12, 22, 27],
    }
)
print(df.groupby(by=["Fruit", "Date"]).sum().groupby(level=[0]).cumsum())

Output:

Fruit  Date         Sale
Apple  08/09/2018    34
       10/09/2018    46
Banana 08/09/2018    22
       10/09/2018    49

pivot()Rearrange data in a nice table

pivot()The method can set the table's row and column properties. Let's change the above code and apply pivot()the method to rearrange the data in a beautiful table:

import pandas as pd

df = pd.DataFrame(
    {
        "Date": ["08/09/2018", "10/09/2018", "08/09/2018", "10/09/2018"],
        "Fruit": [
            "Apple",
            "Apple",
            "Banana",
            "Banana",
        ],
        "Sale": [34, 12, 22, 27],
    }
)
print(
    df.groupby(["Fruit", "Date"], as_index=False).sum().pivot("Fruit", "Date").fillna(0)
)

Output:

             Sale           
Date   08/09/2018 10/09/2018
Fruit                       
Apple          34         12
Banana         22         27

Applying functions to Pandasgroupby

We'll create a simple method to get seriesthe count of values ​​in a , or one-dimensional array, and use groupbyto get the total count for each value:

from pandas import *

d = {"series": Series(["1", "2", "1", "1", "4", "4", "5"])}
df = DataFrame(d)


def get_count(values):
    return len(values)


grouped_count = df.groupby("series").series.agg(get_count)
print(grouped_count)

After running the code, we will get the following output, which provides seriesthe number of occurrences of each value in .

Output:

series
1    3
2    1
4    2
5    1
Name: series, dtype: int64

agg()Get the sum of a column

We can use agg()to apply a sum operation on a column. Consider the following code:

import pandas as pd

df = pd.DataFrame(
    {
        "Date": ["08/09/2018", "10/09/2018", "08/09/2018", "10/09/2018"],
        "Fruit": [
            "Apple",
            "Apple",
            "Banana",
            "Banana",
        ],
        "Sale": [34, 12, 22, 27],
    }
)
print(df.groupby(["Fruit"])["Sale"].agg("sum"))

Output:

Fruit
Apple     46
Banana    49
Name: Sale, dtype: int64

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to Convert DataFrame Column to String in Pandas

Publish Date:2025/05/02 Views:161 Category:Python

We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a

How to count the frequency of values in a Pandas DataFrame

Publish Date:2025/05/02 Views:84 Category:Python

Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().

How to get value from Pandas DataFrame cell

Publish Date:2025/05/02 Views:147 Category:Python

We'll look at using to get values ​​from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .

How to Add a Row to a Pandas DataFrame

Publish Date:2025/05/02 Views:127 Category:Python

Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to

How to change the order of Panas DataFrame columns

Publish Date:2025/05/02 Views:184 Category:Python

We will show how to use insert and reindex to change the order of columns in different ways pandas.DataFrame , such as assigning column names in a desired order. pandas.DataFrame Sort the columns in the new order The easiest way is columns

How to pretty print an entire Pandas Series/DataFrame

Publish Date:2025/05/02 Views:167 Category:Python

We will introduce various methods to pretty print the entire Pandas Series/DataFrame, such as option_context, set_option, and options.display. option_context Pretty Printing Pandas DataFrame We can option_context use with one or more option

How to Convert a Pandas Dataframe to a NumPy Array

Publish Date:2025/05/02 Views:151 Category:Python

We will introduce to_numpy() the method to pandas.Dataframe convert a to NumPy an array, which is introduced in pandas v0.24.0, replacing the old .values method. We can define it on Index , Series , and DataFrame objects to_numpy . The old

How to add a header row to a Pandas DataFrame

Publish Date:2025/05/02 Views:161 Category:Python

We will look at methods for adding a header row to a pandas dataframe, as well as the option to pass in the names directly in the dataframe or by assigning the column names in a list directly to dataframe.columns the method. We will also in

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial