This is my df which consists of 3 columns. I roughly know how to split strings into a new line using stack and unstack. However, I'm wondering how I can retain the "prefix" (which might not always be the same length) when splitting the string.
Edit: Currently I am working with the Pandas version 0.23.0 without the explode function.
Before:
Col1 Col2 Col3
1 QQ12345-01/02/03 x
2 QQ123456-01/02 y
3 QQ12345-01/02/03 z
After:
Col1 Col2 Col3
1 QQ12345-01 x
1 QQ12345-02 x
1 QQ12345-03 x
2 QQ123456-01 y
2 QQ123456-02 y
3 QQ12345-01 z
3 QQ12345-02 z
3 QQ12345-03 z
Currently, I can only manage to split by '/' this is my code below. I Appreciate any help on this.
column_list = df.loc[:,df.columns!='Col2'].columns.tolist()
df.set_index(column_list).stack().str.split('\',expand=True).stack().unstack(-2).reset_index(-1,drop=True).reset_index()