I am trying to ungroup a concatenated column in a dataframe. In particular, I am trying to convert
a b c
i0 1 a k1;k2
i1 2 b k3
i2 3 c k4;k5;k6
i3 4 d k7
into
a b c
i0 1 a k1
i0 1 a k2
i1 2 b k3
i2 3 c k4
i2 3 c k5
i2 3 c k6
i3 4 d k7
I managed to do this using the code
import pandas as pd
data = pd.DataFrame({'a':[1,2,3,4],'b':list('abcd'),'c':['k1;k2','k3','k4;k5;k6','k7']},
index=['i'+str(i) for i in range(4)])
tmp = data['c'].str.split(';', expand=True).stack().reset_index(level=1, drop=True)
tmp.name = 'c'
data.drop('c',axis='columns',inplace=True)
data = data.join(tmp)
but it seems an incredibly convoluted way of doing something that is so simple. Is there a better way to do this using pandas?