Pandas - combine column values into a list in a new column

前端 未结 1 1980
半阙折子戏
半阙折子戏 2021-01-31 09:30

I have a Python Pandas dataframe df:

d=[[\'hello\',1,\'GOOD\',\'long.kw\'],
   [1.2,\'chipotle\',np.nan,\'bingo\'],
   [\'various\',np.nan,3000,123.456]]                 


        
1条回答
  •  清歌不尽
    2021-01-31 10:18

    try this :

    t['combined']= t.values.tolist()
    
    t
    Out[50]: 
             A         B     C        D                       combined
    0    hello         1  GOOD  long.kw      [hello, 1, GOOD, long.kw]
    1     1.20  chipotle   NaN    bingo    [1.2, chipotle, nan, bingo]
    2  various       NaN  3000   123.46  [various, nan, 3000, 123.456]
    

    0 讨论(0)
提交回复
热议问题