I think this is similar to this post but not exactly the same and I cannot get my head around it.
So, I currently have a (quite weird) pandas dataframe with lists in each cell like this:
>>> data = pd.DataFrame({'myid' : ['1', '2', '3'],
'num' : [['1', '2', '3'], ['1', '2'], []],
'text' : [['aa', 'bb', 'cc'], ['cc', 'dd'],
[]]}).set_index('myid')
>>> print(data)
num text
myid
1 [1, 2, 3] [aa, bb, cc]
2 [1, 2] [cc, dd]
3 [] []
I would like to achieve this:
myid num text
0 1 1 aa
0 1 2 bb
0 1 3 cc
1 2 1 cc
1 2 2 dd
2 3
How do I get there?
I'd use str.len
to determine lengths of imbedded lists/arrays. Then use repeat
and concatenate
lens = df.num.str.len()
pd.DataFrame(dict(
myid=df.myid.repeat(lens),
num=np.concatenate(df.num),
text=np.concatenate(df.text)
)).append(
pd.DataFrame(
df.loc[~df.num.astype(bool), 'myid']
)
).fillna('')
myid num text
0 1 1 aa
0 1 2 bb
0 1 3 cc
1 2 1 cc
1 2 2 dd
2 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With