Home >Backend Development >Python Tutorial >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:
3. Unstack and Reset Index:
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!