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

后端 未结 4 1981
栀梦
栀梦 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:15

    We concatenate lists with the + operator. Because a pandas series uses its' elements underlying + operation when you call pd.Series.sum, we can concatenate a whole column, or series, of lists with.

    df.column.sum()
    
    [1, 2, 3, 1, 2]
    

    But if you're looking for performance, you can consider cytoolz.concat

    import cytoolz
    
    list(cytoolz.concat(df.column.values.tolist()))
    
    [1, 2, 3, 1, 2]
    

提交回复
热议问题