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

前端 未结 5 1239
旧时难觅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]]
    

提交回复
热议问题