2017-01-27 80 views
1

我有這個常量值和我的結果列表的列表。我需要將常量及其相應的列表添加到pandas數據框中的一行中。數據框將有2列 - Col1和Col2。我在for循環中生成這些值。用於生成值將值追加到熊貓數據框中

編碼:生成

 for key, elem in dict.items(): 
     print key 
     length = len(elem) 
     elements = list(elem) 
     values = [] 
     firsthalf = elements[:len(elemlist)/2] 
     print firsthalf 

值:

[[0.040456528559673702, -0.085805111083485666]] 
11 
----- 
[[0.035220881869308676, -0.063623927372217309, 0.0063355856789509323]] 
12 

數據幀:

 Col1                 Col2 
     [[0.040456528559673702, -0.085805111083485666]]       11 
     [[0.035220881869308676, -0.063623927372217309, 0.0063355856789509323]] 12 

任何幫助,將不勝感激。謝謝 !!

+0

可能重複:http://stackoverflow.com/questions/41888080/python-efficient-way-to-add-行至非數據幀/ 41888241#41888241 – sundance

回答

1

這是最簡單的你的對象添加到列表中,然後使用這些初始化:

import pandas as pd 
col1 = [] 
col2 = [] 
for key, elem in dict.items(): 
    length = len(elem) 
    elements = list(elem) 
    values = [] 
    firsthalf = elements[:len(elemlist)/2] # elemlist? 
    col1.append(key) 
    col2.append(firsthalf) 

df = pd.DataFrame({'col1': col1, 'col2': col2})