How to Unnest a Column into Multiple Rows in a Pandas DataFrame
Problem:
You have a DataFrame where one column contains a list of values, and you want to separate each list element into its own row.
Solution:
There are several methods to unnest (or explode) a column in a Pandas DataFrame:
Method 1: Using explode (Pandas >= 0.25)
If you have a single column to unnest, the explode function is the simplest solution:
Method 2: Using apply and pd.Series
This method is straightforward but not recommended for performance reasons:
Method 3: Using repeat and DataFrame Constructor
Create a new DataFrame with the repeated values in the unnested column:
Method 4: Using reindex or loc
Create a new DataFrame with the unnested values and use reindex or loc to align it with the original:
Method 5: Using collections.ChainMap (when list contains unique values)
Method 6: Using Numpy for High Performance
This method is more efficient than the previous ones:
Method 7: Using itertools.cycle and itertools.chain
Pure Python solution for fun:
Generalizing to Multiple Columns:
The following function allows you to unnest multiple columns in a DataFrame:
Column-wise Unnesting:
If you need to unnest a column horizontally, use the add_prefix method of the DataFrame constructor:
The above is the detailed content of How to Unnest a Pandas DataFrame Column into Multiple Rows?. For more information, please follow other related articles on the PHP Chinese website!