2015-06-04 280 views
0

我被困在試圖將我的數組轉換成熊貓數據框。Python:陣列(矩陣)到熊貓DataFrame

我的輸出數組是這樣的:

[[1,2,4,n],[1,2,3,n]] 

輸出示例:

[[0.04376367614879647, 0.04376367614879649, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.08096280087527355, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.04376367614879651, 0.043763676148796504, 0.043763676148796504], [0.04376367614879647, 0.04376367614879649, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.08096280087527355, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.043763676148796504, 0.04376367614879651, 0.043763676148796504, 0.043763676148796504],...[]] 

現在我想創建針對每個1..n值列的數據幀。 告訴大熊貓做這件事的最好方法是什麼?

my_dataframe = pd.DataFrame(my_array,columns=['first','second',...n] 

我無法重塑這個my_array,使大熊貓能夠理解。

非常感謝您的幫助

回答

2

有一個.T()方法做的:

In [8]: 

arr = [[1,2,3,5],[2,3,4,6]] 
print pd.DataFrame(arr, index=['col1','col2']).T 
    col1 col2 
0  1  2 
1  2  3 
2  3  4 
3  5  6 

如果輸入是numpyarray

In [9]: 

arr = np.array([[1,2,3,5],[2,3,4,6]]) 
print pd.DataFrame(arr.T, columns=['col1','col2']) 
    col1 col2 
0  1  2 
1  2  3 
2  3  4 
3  5  6 
0

也許我上次沒有正確的列數。它給了我0和1列。現在工作。我想這是問題所在。感謝您的幫助。