How to Split Comma-Separated Values into Multiple Rows in Pandas DataFrames?

Mary-Kate Olsen
Release: 2024-10-28 08:40:03
Original
635 people have browsed it

How to Split Comma-Separated Values into Multiple Rows in Pandas DataFrames?

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>
Copy after login

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>
Copy after login

Details:

Both methods involve several steps:

  • Set non-splitting columns as the index.
  • Split values on commas using str.split.
  • Stack the split values into rows.
  • Unstack to move the split values into separate columns.
  • Reset the final index.

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!