Grouping in Pandas

Grouping is used to group data using some criteria from our dataset. It is used as split-apply-combine strategy.

  • Splitting the data into groups based on some criteria.
  • Applying a function to each group independently.
  • Combining the results into a data structure.

Examples:

We use groupby() function to group the data on “Maths” value. It returns the object as result.

Python




df.groupby(by=['Maths'])


Output:

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x0000012581821388>

Applying groupby() function to group the data on “Maths” value. To view result of formed groups use first() function.

Python




a = df.groupby('Maths')
a.first()


Output:

First grouping based on “Maths” within each team we are grouping based on “Science” 

Python




b = df.groupby(['Maths', 'Science'])
b.first()


Output:

Grouping and Aggregating with Pandas

In this article, we are going to see grouping and aggregating using pandas. Grouping and aggregating will help to achieve data analysis easily using various functions. These methods will help us to the group and summarize our data and make complex analysis comparatively easy.  

Creating a sample dataset of marks of various subjects.

Python




# import module
import pandas as pd
  
# Creating our dataset
df = pd.DataFrame([[9, 4, 8, 9],
                   [8, 10, 7, 6],
                   [7, 6, 8, 5]],
                  columns=['Maths''English'
                           'Science', 'History'])
  
# display dataset
print(df)


Output:

Similar Reads

Aggregation in Pandas

...

Grouping in Pandas

Aggregation in pandas provides various functions that perform a mathematical or logical operation on our dataset and returns a summary of that function. Aggregation can be used to get a summary of columns in our dataset like getting sum, minimum, maximum, etc. from a particular column of our dataset. The function used for aggregation is agg(), the parameter is the function we want to perform....

Implementation on a Dataset

...