# Pandas Groupby and Sum

## Pandas Groupby and Sum

Pandas is an open-source library that is built on top of NumPy library. It is a Python package that offers various data structures and operations for manipulating numerical data and time series. It is mainly popular for importing and analyzing data much easier. Pandas is fast and it has high-performance & productivity for users.

Groupby is a pretty simple concept. We can create a grouping of categories and apply a function to the categories. It’s a simple concept but it’s an extremely valuable technique that’s widely used in data science. It is helpful in the sense that we can :

• Compute summary statistics for every group
• Perform group-specific transformations
• Do the filtration of data

The groupby() involves a combination of splitting the object, applying a function, and combining the results. This can be used to group large amounts of data and compute operations on these groups such as sum().

#### Below are some examples which implement the use of groupby().sum() in pandas module:

Example 1:

 `# import required module` `import` `pandas as pd` ` ` `# create dataframe` `df ``=` `pd.DataFrame({``'Animal'``: [``'Falcon'``, ``'Falcon'``, ``'Parrot'``, ``'Parrot'``],` `                   ``'Max Speed'``: [``380.``, ``370.``, ``24.``, ``26.``]})` ` ` `# use groupby() to compute sum` `df.groupby([``'Animal'``]).``sum``()`

Output

Example 2:

 `# import required module` `import` `pandas as pd` ` ` `# assign list` `l ``=` `[[``100``, ``200``, ``300``], [``10``, ``None``, ``40``],` `     ``[``20``, ``10``, ``30``], [``100``, ``200``, ``200``]]` ` ` `# create dataframe` `df ``=` `pd.DataFrame(l, columns``=``[``"a"``, ``"b"``, ``"c"``])` ` ` `# use groupby() to generate sum` `df.groupby(by``=``[``"b"``]).``sum``()`

Output:

Example 3:

 `# import required module` `import` `pandas as pd` ` ` `# assign data` `ipl_data ``=` `{``'Team'``: [``'Riders'``, ``'Riders'``, ``'Devils'``, ``'Devils'``,` `                     ``'Kings'``,  ``'kings'``, ``'Kings'``, ``'Kings'``,` `                     ``'Riders'``, ``'Royals'``, ``'Royals'``, ``'Riders'``],` `            ``'Rank'``: [``1``, ``2``, ``2``, ``3``, ``3``, ``4``, ``1``, ``1``, ``2``, ``4``, ``1``, ``2``],` ` ` `            ``'Year'``: [``2014``, ``2015``, ``2014``, ``2015``, ``2014``, ``2015``, ``2016``, ` `                     ``2017``, ``2016``, ``2014``, ``2015``, ``2017``],` ` ` `            ``'Points'``: [``876``, ``789``, ``863``, ``673``, ``741``, ``812``, ``756``, ``788``, ` `                       ``694``, ``701``, ``804``, ``690``]}` ` ` `# create dataframe` `df ``=` `pd.DataFrame(ipl_data)` ` ` `# use groupby() to generate sum` `df.groupby([``'Team'``]).``sum``()`

Output:

Last Updated on October 19, 2021 by admin

## How to plot a Pandas Dataframe with Matplotlib?How to plot a Pandas Dataframe with Matplotlib?

How to plot a Pandas Dataframe with Matplotlib? Data visualization is the most important part

## Python – Working with date and time using PandasPython – Working with date and time using Pandas

Python | Working with date and time using Pandas While working with data, encountering time

## Insert row at given position in Pandas DataframeInsert row at given position in Pandas Dataframe

Insert row at given position in Pandas Dataframe Inserting a row in Pandas DataFrame is a very

## Pandas – Practice Excercises, Questions and SolutionsPandas – Practice Excercises, Questions and Solutions

Pandas – Practice Excercises, Questions and Solutions Pandas is an open-source library that is made

## Pandas Index.valuesPandas Index.values

Python | Pandas Index.values Pandas Index is an immutable ndarray implementing an ordered, sliceable set.

## Ways to filter Pandas DataFrame by column valuesWays to filter Pandas DataFrame by column values

Ways to filter Pandas DataFrame by column values In this post, we will see different

## Pandas DataFrame.ix[ ]Pandas DataFrame.ix[ ]

Pandas DataFrame.ix[ ] Pandas DataFrame.ix[ ] is both Label and Integer based slicing technique. Besides

## Python – Scaling numbers column by column with PandasPython – Scaling numbers column by column with Pandas

Python – Scaling numbers column by column with Pandas Scaling numbers in machine learning is