2016-07-17 76 views
1

我有df1的索引有df1.index.shape(80,)。我有一個numpy數組df2它有df2.shape(80,2)。然而,當我嘗試df2轉換成數據幀,df2 = pd.DataFrame(df2,index=df1.index),我收到以下錯誤信息:從一個df到另一個df應用索引的尺寸不匹配

ValueError: Shape of passed values is (80, 2), indices imply (80, 80)

爲什麼我收到此錯誤?任何幫助表示讚賞。

回答

0

對我來說,它的工作原理很好:

import pandas as pd 


df1 = pd.DataFrame({'A':[1,2,3], 
        'B':[4,5,6]}, index=['a','b','c']) 

print (df1) 
    A B 
a 1 4 
b 2 5 
c 3 6 

print (df1.index.shape) 
(3,) 

df2 = df1.values 
print (df2) 
[[1 4] 
[2 5] 
[3 6]] 

print (df2.shape) 
(3, 2) 

print (pd.DataFrame(df2,index=df1.index)) 
    0 1 
a 1 4 
b 2 5 
c 3 6 

如果變化indexcolumns,它引發錯誤:

print (pd.DataFrame(df2,columns=df1.index)) 
ValueError: Shape of passed values is (2, 3), indices imply (3, 3) 

它產生錯誤,如果index長度是不同的:

import pandas as pd 


df1 = pd.DataFrame({'A':[1,2], 
        'B':[3,4], 
        'C':[5,6]}, index=['a','b']) 

print (df1) 
    A B C 
a 1 3 5 
b 2 4 6 

idx = pd.Index([5,6,7]) 
print (idx) 
Int64Index([5, 6, 7], dtype='int64') 

print (idx.shape) 
(3,) 

df2 = df1.values 
print (df2) 
[[1 3 5] 
[2 4 6]] 

print (df2.shape) 
(2, 3) 

print (pd.DataFrame(df2,index=idx)) 
ValueError: Shape of passed values is (3, 2), indices imply (3, 3) 
0

請提供樣本數據集,因爲一切正在這個簡單的例子中工作:

In [15]: df1 = pd.DataFrame(np.random.randint(0, 1000, (5, 4))) 

In [16]: df2 = np.random.rand(5, 2) 

In [17]: df1 
Out[17]: 
    0 1 2 3 
0 724 639 288 169 
1 49 271 75 161 
2 728 329 78 23 
3 586 407 407 390 
4 132 661 704 273 

In [18]: df2 
Out[18]: 
array([[ 0.19690057, 0.64171029], 
     [ 0.61680318, 0.64391293], 
     [ 0.4638142 , 0.99877559], 
     [ 0.12193726, 0.76737424], 
     [ 0.34726316, 0.68705751]]) 

In [19]: pd.DataFrame(df2,index=df1.index) 
Out[19]: 
      0   1 
0 0.196901 0.641710 
1 0.616803 0.643913 
2 0.463814 0.998776 
3 0.121937 0.767374 
4 0.347263 0.687058 
相關問題