如何将列表列表转换为唯一的Pandas DataFrame列?

时间:2018-01-24 15:23:09

标签: python pandas dataframe merge

列表如下:

CREATE DEFINER=`root`@`%.zamberlan.local` PROCEDURE `AggiornaCelle`()
BEGIN
declare i int;
declare num_rows int;
declare col_name varchar(20);
DECLARE col_names CURSOR FOR
  SELECT column_name
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_name = quadri
  ORDER BY ordinal_position;


select FOUND_ROWS() into num_rows;

SET i = 1;
the_loop: LOOP

   IF i > num_rows THEN
        CLOSE col_names;
        LEAVE the_loop;
    END IF;


    FETCH col_names 
    INTO col_name;     

     update celle set needed=sum(col_name) where cellaname=col_name;

    SET i = i + 1;  
END LOOP the_loop;
END

我想将L = [[0,1,1,0], [0,1,1,1], [1,0,0,1], [1,1,0,0], ] 作为:

DataFrame

原因是每个单独的列表本身就是一个对象。

2 个答案:

答案 0 :(得分:0)

您必须将列表读作由值列表形成的索引dictionary

import pandas as pd

L = [[0,1,1,0],
     [0,1,1,1],
     [1,0,0,1],
     [1,1,0,0],
    ]

Df = pd.DataFrame({i:[vals] for i,vals in enumerate(L)},index=['Column Name']).T

它会返回:

    Column Name                                                                                                                                        
0  [0, 1, 1, 0]                                                                                                                                        
1  [0, 1, 1, 1]                                                                                                                                        
2  [1, 0, 0, 1]                                                                                                                                        
3  [1, 1, 0, 0]   

答案 1 :(得分:0)

这是一个直接的解决方案:

import pandas as pd

L = [[0,1,1,0],
     [0,1,1,1],
     [1,0,0,1],
     [1,1,0,0],
    ]

df = pd.DataFrame([[L[i]] for i in range(len(L))], columns=['Column Name'])

# output
# Column Name
# [0, 1, 1, 0] 
# [0, 1, 1, 1] 
# [1, 0, 0, 1] 
# [1, 1, 0, 0]