2016-05-11 43 views
0

我正在嘗試創建一個數據幀,其中第一列是一個令牌列表,並且可以添加其他列信息。不過,熊貓不會允許將一個標記列表添加爲一列。 所以代碼看起來如下將令牌列表作爲一行分配給數據幀

array1 = ['two', 'sample', 'statistical', 'inferences', 'includes'] 
    array2 = ['references', 'please', 'see', 'next', 'page', 'the','material', 'of', 'these'] 
    array3 = ['time', 'student', 'interest', 'and', 'lecturer', 'preference', 'other', 'topics'] 


## initialise list 
list = [] 
list.append(array1) 
list.append(array2) 
list.append(array3) 

## create dataFrame 
numberOfRows = len(list) 
df = pd.DataFrame(index=np.arange(0, numberOfRows), columns = ('data', 'diversity')) 

df.iloc[0] = list[0] 

錯誤信息讀取

ValueError: cannot copy sequence with size 6 to array axis with dimension 2 

任何深入瞭解我怎樣才能更好地實現建立一個數據幀和更新列,將不勝感激。 謝謝

+0

是關於'df = pd.DataFrame({'data':list}); df ['diversity'] ='?''實際上不要使用像'list'這樣的保留字作爲變量! – MaxU

回答

0

好吧,所以答案很簡單,張貼繁榮。 將列表添加爲行時,我需要包含列名和位置。 所以代碼如下所示。

df.data[0] = array1 
相關問題