Home > Backend Development > Python Tutorial > How to Explode Nested Lists into Individual Rows in a Pandas Dataframe?

How to Explode Nested Lists into Individual Rows in a Pandas Dataframe?

Mary-Kate Olsen
Release: 2024-11-12 03:10:02
Original
528 people have browsed it

How to Explode Nested Lists into Individual Rows in a Pandas Dataframe?

Exploding Nested Lists Into Individual Rows in a Pandas Dataframe

In the realm of data manipulation with pandas, the need often arises to restructure data stored as nested lists into individual rows. Consider a dataframe where the "nearest_neighbors" column contains lists of values. The objective is to "explode" these lists, creating separate rows for each value within the list.

Pandas 0.25 Simplifies List Exploding with explode() Method

For pandas versions 0.25 and later, expanding lists in columns is significantly simplified with the introduction of the explode() method. To demonstrate its functionality, let's recreate the example dataframe:

import pandas as pd

# Original DataFrame
df = pd.DataFrame({'name': ['A.J. Price'] * 3, 
                    'opponent': ['76ers', 'blazers', 'bobcats'], 
                    'nearest_neighbors': [['Zach LaVine', 'Jeremy Lin', 'Nate Robinson', 'Isaia']] * 3})

# Set the index for easier reference
df = df.set_index(['name', 'opponent'])
Copy after login

Exploding the Nested Lists

Using the explode() method, we can split the "nearest_neighbors" column by its list elements, creating separate rows for each value:

# Explode the list-like column
df_exploded = df.explode('nearest_neighbors')
Copy after login

Output after Exploding

print(df_exploded)
Copy after login
                    nearest_neighbors
name       opponent                  
A.J. Price 76ers          Zach LaVine
           76ers           Jeremy Lin
           76ers        Nate Robinson
           76ers                Isaia
           blazers        Zach LaVine
           blazers         Jeremy Lin
           blazers      Nate Robinson
           blazers              Isaia
           bobcats        Zach LaVine
           bobcats         Jeremy Lin
           bobcats      Nate Robinson
           bobcats              Isaia
Copy after login

As you can see, each value from the list in the "nearest_neighbors" column is now represented as a separate row within its corresponding opponent index.

Other Methods for List Expansion

For pandas versions prior to 0.25, there were other approaches to expand lists in columns. These methods required a combination of operations like apply, lambda, and list comprehension. However, with the introduction of the explode() method, these more complex approaches are no longer necessary.

The above is the detailed content of How to Explode Nested Lists into Individual Rows in a Pandas Dataframe?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template