How can I convert columns of a pandas DataFrame into a list of lists?

前端 未结 2 1017

I have a pandas DataFrame with multiple columns.

2u    2s    4r     4n     4m   7h   7v
0     1     1      0      0     0    1
0     1     0      1      0            


        
相关标签:
2条回答
  • 2020-12-13 02:33

    To change Dataframe into list use tolist() function to convert Let use say i have Dataframe df

    to change into list you can simply use tolist() function

    df.values.tolist()
    

    You can also change a particular column in to list by using

    df['column name'].values.tolist()
    
    0 讨论(0)
  • 2020-12-13 02:53

    It looks like a transposed matrix:

    df.values.T.tolist()
    

    [list(l) for l in zip(*df.values)]
    

    [[0, 0, 1, 1, 1, 0],
     [1, 1, 0, 0, 0, 1],
     [1, 0, 0, 0, 1, 1],
     [0, 1, 1, 0, 0, 0],
     [0, 0, 0, 1, 0, 0],
     [0, 0, 1, 1, 1, 0],
     [1, 1, 0, 0, 0, 1]]
    
    0 讨论(0)
提交回复
热议问题