Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced Python pandas reshape

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?

like image 301
absurd Avatar asked Feb 04 '23 14:02

absurd


1 Answers

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         
like image 167
piRSquared Avatar answered Feb 08 '23 08:02

piRSquared