Pandas DataFrame Groupby Method

PandasPandasBeginner
Practice Now

Introduction

In this lab, we will learn how to use the groupby() method in the Pandas library in Python. The groupby() method allows us to split a DataFrame into groups and perform calculations or statistics on each group. It is a powerful tool for data analysis and manipulation.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) pandas(("`Pandas`")) -.-> pandas/DataAnalysisGroup(["`Data Analysis`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") pandas/DataAnalysisGroup -.-> pandas/basic_statistics("`Basic Statistics`") pandas/DataAnalysisGroup -.-> pandas/groupby_operations("`GroupBy Operations`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills pandas/select_columns -.-> lab-68630{{"`Pandas DataFrame Groupby Method`"}} pandas/basic_statistics -.-> lab-68630{{"`Pandas DataFrame Groupby Method`"}} pandas/groupby_operations -.-> lab-68630{{"`Pandas DataFrame Groupby Method`"}} python/lists -.-> lab-68630{{"`Pandas DataFrame Groupby Method`"}} python/tuples -.-> lab-68630{{"`Pandas DataFrame Groupby Method`"}} python/dictionaries -.-> lab-68630{{"`Pandas DataFrame Groupby Method`"}} python/importing_modules -.-> lab-68630{{"`Pandas DataFrame Groupby Method`"}} python/numerical_computing -.-> lab-68630{{"`Pandas DataFrame Groupby Method`"}} python/data_analysis -.-> lab-68630{{"`Pandas DataFrame Groupby Method`"}} python/build_in_functions -.-> lab-68630{{"`Pandas DataFrame Groupby Method`"}} end

Import the necessary libraries and create the DataFrame

First, we need to import the Pandas library and create a DataFrame object. Here is an example:

import pandas as pd

data = {'Name': ['Avinash', 'Amrutha', 'Chetana', 'Kartik','Nikhil'],
        'Percentage': [72, 98, 81, 87,85],
        'Course': ['Arts','B.Com','M.Tech','B.SC','BE']}

df = pd.DataFrame(data)

Group the DataFrame by a single column

To group the DataFrame by a single column, use the groupby() method and specify the column name as the argument. Here is an example:

grp = df.groupby('Course')

Access the groups

To access the groups in the grouped DataFrame, use the groups attribute. It returns a dictionary where the keys are the group names and the values are the corresponding indices of the rows in each group. Here is an example:

print(grp.groups)

Group the DataFrame by multiple columns

To group the DataFrame by multiple columns, pass a list of column names to the groupby() method. Here is an example:

grp = df.groupby(['Course', 'Name'])

Select a single group

To select a single group from the grouped DataFrame, use the get_group() method and specify the group name as the argument. Here is an example:

print(grp.get_group(('Arts', 'Avinash')))

Perform aggregation operations

Once you have a grouped DataFrame, you can perform aggregation operations on the grouped data. For example, you can calculate the mean of a numerical column for each group. Here is an example:

print(grp['Percentage'].mean())

Summary

In this lab, we learned how to use the groupby() method in the Pandas library to group a DataFrame by one or more columns. We also learned how to access the groups, select a single group, and perform aggregation operations on the grouped data. The groupby() method is a powerful tool for data analysis and manipulation, enabling us to gain insights from our data by analyzing it in groups.

Other Pandas Tutorials you may like