I have a dataframe df
:
0 1 2
Mon ['x','y','z'] ['a','b','c'] ['a','b','c']
Tue ['a','b','c'] ['a','b','c'] ['x','y','z']
Wed ['a','b','c'] ['a','b','c'] ['a','b','c']
Lists are all of diff from each other (Maybe similar too) and I wish to convert it to the form:
0 1 2
Mon x a a
Mon y b b
Mon z c c
Tue a a x
Tue b b y
Tue c c z
Wed a a a
Wed b b b
Wed c c c
Referring to some previous SO questions, Explode lists with different lengths in Pandas, Split (explode) pandas dataframe string entry to separate rows
I have tried to use their solutions but I am unable to get the desired output. How can I achieve this?
s1 = df[0]
s2 = df[1]
s3 = df[2]
i1 = np.arange(len(df)).repeat(s1.str.len())
i2 = np.arange(len(df)).repeat(s2.str.len())
i3 = np.arange(len(df)).repeat(s3.str.len())
df.iloc[i1, :-1].assign(**{'Shared Codes': np.concatenate(s1.values)})
df.iloc[i2, :-1].assign(**{'Shared Codes': np.concatenate(s2.values)})
df.iloc[i3, :-1].assign(**{'Shared Codes': np.concatenate(s3.values)})
Also, this doesn't seem like a very reasonable way to do it, provided I have even more columns. Using python 2.7.