Home  >  Article  >  Backend Development  >  How to Split a Cell into Multiple Rows in a Pandas Dataframe Based on Comma-Separated Values?

How to Split a Cell into Multiple Rows in a Pandas Dataframe Based on Comma-Separated Values?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-28 06:41:30660browse

How to Split a Cell into Multiple Rows in a Pandas Dataframe Based on Comma-Separated Values?

Split Cell into Multiple Rows in Pandas Dataframe

Problem:

You have a Pandas dataframe with orders data where each order contains multiple packages stored as comma-separated strings in the 'package' and 'package_code' columns. You aim to split the packages data and create a new row for each package with its corresponding order details.

Solution:

For Pandas versions >= 0.25:

<code class="python">df.set_index(['order_id', 'order_date']) \
    .apply(lambda x: x.str.split(',').explode()) \
    .reset_index()</code>

For Pandas versions <= 0.24:

<code class="python">df.set_index(['order_date', 'order_id']) \
    .stack() \
    .str.split(',', expand=True) \
    .stack() \
    .unstack(-2) \
    .reset_index(-1, drop=True) \
    .reset_index()

Explanation:

1. Set Index: Set the columns that should remain intact ('order_id' and 'order_date') as the index of the dataframe.

2. Split and Stack:

  • For Pandas >= 0.25: Use apply(lambda x: x.str.split(',').explode()) to split the comma-separated 'package' and 'package_code' columns and expand them into multiple rows.
  • For Pandas <= 0.24: Use stack(), str.split(), and stack() again to split and stack the data.
  • 3. Unstack and Reset Index:

    • Unstack on the second last level (-2) to create new columns for the split package and package code data.
    • Reset the index to revert the index change and obtain the final dataframe.

    The above is the detailed content of How to Split a Cell into Multiple Rows in a Pandas Dataframe Based on Comma-Separated Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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