Pandas, DF.groupby().agg(), column reference in agg()

Learn, how can we use DF.groupby().agg(), column reference in agg() in Python pandas? By Pranit Sharma Last updated : October 03, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Suppose we are given a DataFrame with multiple columns and we need to some max value or count for a given column value at some particular time.

Using DF.groupby().agg(), column reference in agg()

The groupby() method is a simple but very useful concept in pandas. By using groupby, we can create a grouping of certain values and perform some operations on those values. It splits the object, applies some operations, and then combines them to create a group hence a large amount of data and computations can be performed on these groups.

Basically, groupby operation packs the data into an object and if we want to check out that data, we need to access that object and we are going to get all the keys to this object.

Let us understand with the help of an example,

Python program to demonsrate how to use DF.groupby().agg(), column reference in agg()

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    'team':['mi','csk','kkr'],
    'win':['yes','yes','yes'],
    'total':[5,4,2]
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Grouping and using aggregate
res = df.groupby('team')['total'].idxmax()

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: DF.groupby().agg(), column reference in agg()

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.