0

I have a df with certain column values that I want to split and then copy into new rows. For example:


Order#              Price             Quantity
123                   $20                5
456,789               $50                7
321                   $45                3
121,651               $76                9

Some rows in the "order#" column contain two order numbers separated by a column. I want each order# to have its own row. For example, I want the second row (456, 789) to be split by the column delimeter into two rows. The final result would look like:

Order#              Price             Quantity
123                   $20                5
456                   $50                7
789                   $50                7
321                   $45                3
121                   $76                9
651                   $76                9

I tried doing something like:

df['Order Column'].str.split(",", expand=True)   

but then got stuck. Appreciate any input.

not_speshal
  • 22,093
  • 2
  • 15
  • 30
Daniel
  • 3
  • 1

1 Answers1

0

You need to explode after split:

df["Order#"] = df["Order#"].str.split(",")

>>> df.explode("Order#")
  Order# Price  Quantity
0    123   $20         5
1    456   $50         7
1    789   $50         7
2    321   $45         3
3    121   $76         9
3    651   $76         9
not_speshal
  • 22,093
  • 2
  • 15
  • 30