Home >Backend Development >Python Tutorial >How to Separate Comma-Separated Values into Multiple Rows in a Pandas DataFrame?
In this scenario, you have a DataFrame that contains orders data, where each order has multiple packages stored as comma-separated strings in the package and package_code columns. The goal is to split these packages and create a new row for each package that includes its corresponding order details.
Assuming all splittable columns have the same number of comma-separated items, you can utilize the following steps to achieve the desired output:
<code class="python">import pandas as pd # Create the sample DataFrame df = pd.DataFrame({"order_id": [1, 3, 7], "order_date": ["20/5/2018", "22/5/2018", "23/5/2018"], "package": ["p1,p2,p3", "p4", "p5,p6"], "package_code": ["#111,#222,#333", "#444", "#555,#666"]}) # Use pandas functions to split and explode the columns result_df = (df.set_index(['order_id', 'order_date']) .apply(lambda x: x.str.split(',').explode()) .reset_index()) # Display the output DataFrame print(result_df)</code>
This code first sets the columns not to be touched as the index. It then iterates through each column, splits the values on commas, and explodes them into their own rows. Finally, the index is reset to obtain the desired format.
The output DataFrame will have a separate row for each package, including its order details.
The above is the detailed content of How to Separate Comma-Separated Values into Multiple Rows in a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!