Home > Backend Development > Python Tutorial > How to Unpack a List-Like Column into Separate Rows in a DataFrame?

How to Unpack a List-Like Column into Separate Rows in a DataFrame?

Linda Hamilton
Release: 2024-11-11 22:51:03
Original
890 people have browsed it

How to Unpack a List-Like Column into Separate Rows in a DataFrame?

Unpacking a List-Like Column into Separate Rows in a DataFrame

Question:

How can we transform a DataFrame cell containing a list into individual rows for each value within that list?

Example:

Consider the following DataFrame:

name opponent nearest_neighbors
A.J. Price 76ers ['Zach LaVine', 'Jeremy Lin', 'Nate Robinson', 'Isaia']

Our goal is to "explode" the nearest_neighbors column, creating a new row for each value in the list.

Answer:

In pandas version 0.25 and later, the explode() method makes this operation straightforward:

import pandas as pd

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

df.explode('nearest_neighbors')
Copy after login

Output:

name opponent nearest_neighbors
A.J. Price 76ers Zach LaVine
A.J. Price 76ers Jeremy Lin
A.J. Price 76ers Nate Robinson
A.J. Price 76ers Isaia
A.J. Price blazers Zach LaVine
A.J. Price blazers Jeremy Lin
A.J. Price blazers Nate Robinson
A.J. Price blazers Isaia
A.J. Price bobcats Zach LaVine
A.J. Price bobcats Jeremy Lin
A.J. Price bobcats Nate Robinson
A.J. Price bobcats Isaia

The explode() method effectively unrolls each list in the nearest_neighbors column, creating a new row for each value.

The above is the detailed content of How to Unpack a List-Like Column into Separate Rows in a 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