How to Group rows in a Pandas DataFrame in Python

Hits: 149

How to Group rows in a Pandas DataFrame in Python

Grouping rows in a Pandas DataFrame in Python is a powerful way to perform aggregations and other data manipulation tasks. The process of grouping rows is also known as “groupby” in Pandas, and it can be used to group rows based on the values in one or more columns. In this blog, we will go over the basic usage of the groupby method and the different ways to use it to group rows in a DataFrame.

The basic syntax for using the groupby method is:



This will group the DataFrame by the values in the “column_name” column. Once the rows are grouped, you can perform various aggregations on the grouped data using built-in methods such as mean(), sum(), count(), min(), max() etc.

For example, you could group a DataFrame by the values in the “City” column, and then find the average “Age” for each city:




This will return a new DataFrame with the average age for each city.

You can also group by multiple columns, to do so, you pass a list of columns names to the groupby method:



This will group the DataFrame by the values in both the “City” and “Gender” columns and then calculate the mean of the “Age” column

Another common use case for grouping rows in a DataFrame is to perform more advanced aggregations. For this, you can use the agg() method. This method takes a dictionary of aggregate functions as an argument, with the keys as the column names and the values as the aggregate function to apply. For example, you could group the DataFrame by the “City” column and calculate the mean, standard deviation, and count of the “Age” column:

df.groupby("City")["Age"].agg({"mean": "mean", "std": "std", "count": "count"})


You can also apply custom aggregate functions, you will need to import numpy and then use it inside the agg() function. For example, you could use numpy’s np.median() function to calculate the median age for each city:

import numpy as np


It’s worth noting that, the groupby method returns a special DataFrameGroupBy object, this object is not a DataFrame, and most of the methods that are applied to it return a new grouped DataFrame. If you want to keep the original DataFrame format, you can use the as_index parameter and set it to False.

In conclusion, grouping rows in a Pandas DataFrame in Python is a powerful technique that allows you to perform various data manipulation tasks such as aggregations and advanced data summarization. With the groupby() method and its associated methods such as mean(), sum(), agg() etc, it’s easy to group rows in a DataFrame based on the values in one or more columns and perform the desired operations.

In this Learn through Codes example, you will learn: How to Group rows in a Pandas DataFrame in Python.

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners