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

前端 未结 5 1232
旧时难觅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:21

    Old question I know, but this makes more sense to me than these other answers.

    If this is your dataframe:

    df = pd.DataFrame({'one': [1, 1], 'three': [3, 1], 'four': [4, 1],
               'five': [5, 1], 'two': [2, 1]},
              columns=['one', 'two', 'three', 'four', 'five'])
    

    Do this:

    df.T.reset_index().values.tolist()
    

    Result

    [['one', 1, 1], ['two', 2, 1], ['three', 3, 1], ['four', 4, 1], ['five', 5, 1]]
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-02-02 14:35

    @BrenBarn answer above yields a list of tuples not a list of list as asked in question. I specifically needed a list of lists to be able to write the dataframe into spreadsheed using DataNitro. Adapted the above example with list comprehension:

    [list(x) for x in dt.T.itertuples()]
    

    This yields the result as needed

    0 讨论(0)
  • 2021-02-02 14:42

    Strictly speaking if you want nested lists (and not a list of tuples) you can do

    df.values.tolist()
    

    as df.values is a numpy array. That will give you a list of lists as requested:

    [[0.0001313652121930252, 3.5915356549999985e-05], 
     [3.5915356549999985e-05, 0.00011634321240684215]]
    
    0 讨论(0)
  • 2021-02-02 14:46

    Simplest way is probably list(dt.T.itertuples()) (where dt is your dataframe). This generates a list of tuples.

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