Iterate over pandas dataframe columns containing nested arrays

前端 未结 4 1707
情话喂你
情话喂你 2021-01-21 01:46

I hope you can help me with this issue,

I\'ve this data below (Columns names whatever)

data=([[\'file0090\',
    ([[ 84,  55, 189],
   [248, 100,  18],
         


        
4条回答
  •  面向向阳花
    2021-01-21 02:20

    You can do explode with a join after crreating another df from the series of lists:

    df = pd.DataFrame(data).add_prefix('col')
    
    out = df.explode('col1').reset_index(drop=True)
    out = out.join(pd.DataFrame(out.pop('col1').tolist()).add_prefix('col_'))
    

    Adding another solution if the list structure is similar:

    l = [*itertools.chain.from_iterable(data)]
    pd.DataFrame(np.vstack(l[1::2]),index = np.repeat(l[::2],len(l[1])))
    

          col0  col_0  col_1  col_2
    0  file0090     84     55    189
    1  file0090    248    100     18
    2  file0090     68    115     88
    3  file6565     86     58    189
    4  file6565     24     10    118
    5  file6565     68     11      8
    

提交回复
热议问题