Pandas offers comprehensive tools for data manipulation, including the ability to split a cell that contains multiple comma-separated values into multiple rows. In this guide, we will explore methods to achieve this using two different approaches based on pandas' version.
For pandas versions 0.25 and above, you can use a combination of apply, str.split, and Series.explode to achieve the desired result. Here's the code snippet:
<code class="python">(df.set_index(['order_id', 'order_date']) .apply(lambda x: x.str.split(',').explode()) .reset_index()) </code>
Explanation:
For pandas versions 0.24 and below, a more complex approach involving stack, unstack, and str.split is necessary:
<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() )</code>
Explanation:
Both methods will return a new DataFrame with the exploded values as separate rows, as illustrated in the desired output you provided.
The above is the detailed content of How can I split a comma-separated cell into multiple rows in a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!