Groupby with User Defined Functions in Pandas

Learn how to use groupby with user defined functions?
Submitted by Pranit Sharma, on November 23, 2022

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.

Using groupby with user defined functions

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.

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 code 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:

Example: Groupby with User Defined Functions in Pandas

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.