


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'])
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')
Output after Exploding
print(df_exploded)
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...

Fastapi ...
