I have a dataframe like this:
--------------------------------------------------------------------
Product ProductType SKU Size
--------------------------------------------------------------------
T-shirt Top [111,222,333,444] ['XS','S','M','L']
Pant(Flared) Bottoms [555,666] ['M','L']
Sweater Top None None
I want the below output:
Product ProductType SKU Size
T-shirt Top 111 XS
T-shirt Top 222 S
T-shirt Top 333 M
T-shirt Top 444 L
Pant(Flared) Bottoms 555 M
Pant(Flared) Bottoms 666 L
Sweater Top None None
I tried the following code:
s = df['SKU'].apply(Series,1).stack()
s.index = s.index.droplevel(-1)
s.name = 'SKU'
del df['SKU']
df = df.join(s)
r = df['Size'].apply(Series,1).stack()
r.index = r.index.droplevel(-1)
r.name = 'Size'
del df['Size']
df = df.join(r)
But this explodes into the following:
Product ProductType SKU Size
T-shirt Top 111 XS
T-shirt Top 111 S
T-shirt Top 111 M
T-shirt Top 111 L
T-shirt Top 222 XS
T-shirt Top 222 S
T-shirt Top 222 M
T-shirt Top 222 L
T-shirt Top 333 XS
T-shirt Top 333 S
T-shirt Top 333 M
T-shirt Top 333 L
T-shirt Top 444 XS
T-shirt Top 444 S
T-shirt Top 444 M
T-shirt Top 444 L
Pant(Flared) Bottoms 555 M
Pant(Flared) Bottoms 555 L
Pant(Flared) Bottoms 666 M
Pant(Flared) Bottoms 666 L
Note that for simplicity sake, I have added two columns that will be repeated (Product, ProductType) but I have 5 such columns that contain strings. I basically want to associate the SKU with the size for each product.
Can anyone help here ?