How to convert list column to a non-nested list?

后端 未结 4 1994
栀梦
栀梦 2021-01-23 03:55

How to convert a column to a non-nested list while the column elements are list?

For example, the column is like

column
[1, 2, 3]
[1, 2]
<
4条回答
  •  醉酒成梦
    2021-01-23 04:24

    You can use numpy.concatenate:

    print (np.concatenate(df['column'].values).tolist())
    [1, 2, 3, 1, 2]
    

    Or:

    from  itertools import chain
    print (list(chain.from_iterable(df['column'])))
    [1, 2, 3, 1, 2]
    

    Another solution, thanks juanpa.arrivillaga:

    print ([item for sublist in df['column'] for item in sublist])
    [1, 2, 3, 1, 2]
    

    Timings:

    df = pd.DataFrame({'column':[[1,2,3], [1,2]]})
    df = pd.concat([df]*10000).reset_index(drop=True)
    print (df)
    
    In [77]: %timeit (np.concatenate(df['column'].values).tolist())
    10 loops, best of 3: 22.7 ms per loop
    
    In [78]: %timeit (list(chain.from_iterable(df['column'])))
    1000 loops, best of 3: 1.44 ms per loop
    
    In [79]: %timeit ([item for sublist in df['column'] for item in sublist])
    100 loops, best of 3: 2.31 ms per loop
    
    In [80]: %timeit df.column.sum()
    1 loop, best of 3: 1.34 s per loop
    

提交回复
热议问题