Pandas GroupBy get list of groups

Learn, how can we get groupby list of groups in pandas DataFrame?
Submitted by Pranit Sharma, on September 13, 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.

GroupBy get list of groups

Suppose, we have a DataFrame with multiple columns along with a specific column called fruits. Fruits contain multiple values. Now after grouping the values of fruits, we need to get the list of all the group values.

The groupby() is a simple but very useful concept in pandas. By using groupby(), we can create grouping of certain values and perform some operations on those values.

The groupby() function split the object, apply some operations, and then combines them to create a group hence a large amount of data and computations can be performed on these groups.

Let us understand with the help of an example,

Python code for Pandas GroupBy get list of groups

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    'A':[1,2,3,4,5,6],
    'B':[1,2,3,4,5,6],
    'fruits':['mango','apple','grape','mango','apple','guava']
}

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

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

# Getting group values of fruits
group = df.groupby('fruits')
group = group.groups.keys()

# Display group values
print("Result:\n",group)

Output:

Example: Pandas GroupBy get list of groups

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.