Groupby with User Defined Functions in Pandas

Learn how to use groupby with user defined functions? By Pranit Sharma Last updated : October 05, 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 the data frame with some columns now we are also defining our function which returns on value based on the data coming inside it.

Now if we apply group by on our data frame and call our self-made function, it is easy to perform this calculation but we need to find a way by which we can call our self-made function by grouping a particular column of the data frame.

Using groupby with user defined functions

For this purpose, we need to pass 3 arguments in our function these arguments would be the dataframe, index, the column. We can then easily apply any condition on the grouped value by accessing the column value of the data frame with the help of its index.

Let us understand with the help of an example

Python program to use group by with user defined functions

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating dataframe
df = pd.DataFrame(np.random.randn(5, 5),
    columns=['a', 'b', 'c', 'd', 'e'],
    index=['Ram','Shyam','Seeta','Geeta','James'])

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

# Defining a function
def Fun(df,ind,col):
    if df[col].loc[ind] > 1:
        return 'Group1'
    else:
        return 'Group2'

# Using groupby and calling the function
res = df.groupby(lambda x: Fun(df, x, 'a')).sum()

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

Output

The output of the above program is:

Example: Groupby with User Defined Functions in Pandas

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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