Say I have the below dataframe:
x = pd.DataFrame({'a':['x, y', 'x, t, x, r', 'y, t'],
'b':[1, 0, 1]})
a b
0 x, y 1
1 x, t, x, r 0
2 y, t 1
I would like to get to
letter num
0 x 1
1 y 1
2 x 0
3 t 0
4 x 0
5 r 0
6 y 1
7 t 1
I have solved the issue the following way, but I feel like i'm making it more complicated than it needs to be.
x.a = x.a.str.split(",")
empty = []
for b, a in zip(x.b, x.a):
empty.append([b] * len(a))
t = [item for sublist in empty for item in sublist]
y = [item for sublist in x.a for item in sublist]
pd.DataFrame({'letter':t, 'num':y})
letter num
0 1 x
1 1 y
2 0 x
3 0 t
4 0 x
5 0 r
6 1 y
7 1 t
Is there a better way to solve this problem?