Splitting Cell into Multiple Rows in Pandas DataFrames
When dealing with comma-separated values in pandas dataframes, converting them into their own rows can be necessary for further analysis. Here's how to achieve this:
For Pandas >= 0.25:
This method simplifies the process:
<code class="python">(df.set_index(['order_id', 'order_date']) .apply(lambda x: x.str.split(',').explode()) .reset_index()) order_id order_date package package_code 0 1 20/5/2018 p1 #111 1 1 20/5/2018 p2 #222 2 1 20/5/2018 p3 #333 3 3 22/5/2018 p4 #444 4 7 23/5/2018 p5 #555 5 7 23/5/2018 p6 #666</code>
For Pandas <= 0.24:
For earlier Pandas versions, a different approach 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() ) order_date order_id package package_code 0 20/5/2018 1 p1 #111 1 20/5/2018 1 p2 #222 2 20/5/2018 1 p3 #333 3 22/5/2018 3 p4 #444 4 23/5/2018 7 p5 #555 5 23/5/2018 7 p6 #666</code>
Details:
Both methods involve several steps:
The above is the detailed content of How to Split Comma-Separated Values into Multiple Rows in Pandas DataFrames?. For more information, please follow other related articles on the PHP Chinese website!