Q:

How to group DataFrame rows into list in pandas groupby?

belongs to collection: Python Pandas Programs

0

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 levelsortas_indexgroup_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

All Answers

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

Let us understand with the help of an example.

# Importing pandas package
import pandas as pd

# Creating a dictionary
dict = {
    'Name':['Harry','Raman','Parth','Mukesh','Neelam','Megha','Deepak','Nitin','Manoj','Rishi','Sandeep','Divyansh','Sheetal','Shalini'],
    'Sport_selected':['Cricket','Cricket','Cricket','Cricket','Basketball','Basketball','Football','Cricket','Tennis','Tennis','Chess','Football','Basketball','Chess']
}

# Creating a DataFrame
df = pd.DataFrame(dict)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Grouping the column named Sport selected with the column Name
result = df.groupby('Sport_selected')['Name'].apply(list)

# Display Result
print("Grouped values, people with particular sports:\n",result)

Output:

Output: Group DataFrame rows into list

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

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to filter pandas DataFrame by operator chainin... >>
<< How to check whether a Pandas DataFrame is empty?...