How to generate a list from a pandas DataFrame with the column name and column values?

前端 未结 5 1244
旧时难觅i
旧时难觅i 2021-02-02 14:00

I have a pandas dataframe object that looks like this:

   one  two  three  four  five
0    1    2      3     4     5
1    1    1      1     1     1
5条回答
  •  你的背包
    2021-02-02 14:28

    My naive approach would be using iteritems with 'll' as a list of lists and l as a single list.

    df = DataFrame({'one':[1,1], 'two':[2,1], 'three':[3,1], 'four':[3,1] })
    
    ll = []
    
    for idx,row in df.iteritems():
        l = row.values.tolist()
        l.insert(0,idx)
        ll.append(l)
    

提交回复
热议问题