My data is like this:
Name test1 test2 test3 Count
Emp1 X,Y A a1,a2 1
Emp2 X A,B,C a3 2
Emp3 Z C a4,a5,a6 3
To split test1 and test2 cells with multiple values to individual rows and merged them together.
df2 = df.test1.str.split(',').apply(pd.Series)
df2.index = df.set_index(['Name', 'Count']).index
df2=df2.stack().reset_index(['Name', 'Count'])
df3 = df.test2.str.split(',').apply(pd.Series)
df3.index = df.set_index(['Name', 'Count']).index
df3=df3.stack().reset_index(['Name', 'Count'])
df2.merge(df3,on=['Name', 'Count'],how='outer')
The out of code is :
Out[132]:
Name Count 0_x 0_y
0 Emp1 1 X A
1 Emp1 1 Y A
2 Emp2 2 X A
3 Emp2 2 X B
4 Emp2 2 X C
5 Emp3 3 Z C
Code to split Test3 with multiple values to individual rows
df4.index = df.set_index(['Name', 'Count']).index
df4=df4.stack().reset_index(['Name', 'Count'])
Can anyone help me, how to multi-join Test3 with test2 and test1 Like i merged Test1 and Test in above code?