Home > Backend Development > Python Tutorial > How to Unnest a Pandas DataFrame Column into Multiple Rows?

How to Unnest a Pandas DataFrame Column into Multiple Rows?

Susan Sarandon
Release: 2024-12-21 00:47:09
Original
507 people have browsed it

How to Unnest a Pandas DataFrame Column into Multiple Rows?

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!

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