0

I am trying to explode a column to create new rows within pandas data frame. What would be the best approach to this?

Input:

SKU Quantity Name
YY-123-671 5 drawer
YY-345-111-WH,YY-345-111-RD,YY-345-111-BL 10 desk
LK-896-001 1 lamp

Desired Output:

SKU Quantity Name
YY-123-671 5 drawer
YY-345-111-WH 10 desk
YY-345-111-RD 10 desk
YY-345-111-BL 10 desk
LK-896-001 1 lamp
Defcon
  • 807
  • 3
  • 15
  • 36

1 Answers1

1
df.assign(SKU=df['SKU'].str.split(',')).explode('SKU')

result:

    SKU             Quantity    Name
0   YY-123-671      5           drawer
1   YY-345-111-WH   10          desk
1   YY-345-111-RD   10          desk
1   YY-345-111-BL   10          desk
2   LK-896-001      1           lamp
Panda Kim
  • 6,246
  • 2
  • 12