DataFrame rows are based on the index values. We can manipulate both rows and columns in pandas. On the other hand, indexes are the integer values representing the number of rows and columns separately. We can perform many operations on rows of a DataFrame based on a specific condition. Suppose, we want to group the DataFrame rows into the list, for this purpose, we will use the groupby() method of Pandas.
pandas.DataFrame.groupby() Method
This method is used to group the data inside DataFrame based on the condition passed inside it as a parameter. It works on a split and group basis. It splits the data and then combines them in the form of a series or any other sequence.
Syntax:
DataFrame.groupby(
by=None,
axis=0,
level=None,
as_index=True,
sort=True,
group_keys=True,
squeeze=NoDefault.no_default,
observed=False,
dropna=True
)
# or
DataFrame.groupby()
Parameter(s):
- by: this parameter is none by default, but it takes a map, function, string, or any other iterable object.
- axis: it is the integer value that is 0 by default.
- It has some other optional parameters like level, sort, as_index, group_keys, and squeeze.
Return value: The method returns a groupby object.
To work with Python Pandas, we need to import the pandas library. Below is the syntax,
import pandas as pd
Let us understand with the help of an example.
Output:

need an explanation for this answer? contact us directly to get an explanation for this answer