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!