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')
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!