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 |
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