How to get unique values from multiple columns in a pandas groupby?

Given a pandas dataframe, we have to get unique values from multiple columns in a pandas groupby.
Submitted by Pranit Sharma, on September 20, 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.

Problem statement

We are supposed to find the unique values from multiple groupby.

Getting unique values from multiple columns in a pandas groupby

For this purpose, we can use the combination of dataframe.groupby() and apply() method with the specified lambda expression. The groupby() method is a simple but very useful concept in pandas. By using this, we can create a grouping of certain values and perform some operations on those values.

Let us understand with the help of an example,

Python program to get unique values from multiple columns in a pandas groupby

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
    'A':[10,10,10,20,20,20],
    'B':['a','a','b','c','c','b'],
    'C':['b','d','d','f','e','f']
}

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

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

# Finding unique values
res = df.groupby('A')['B','C'].apply(lambda x: list(np.unique(x)))

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

Output

The output of the above program is:

Example: Get unique values from multiple columns

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






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