如何在熊猫中将数组更改为数据框

时间:2019-06-05 11:47:29

标签: python arrays pandas dataframe reshape

我正在尝试将数组转换为数据框。这是数组。

Array = 
[[array([[1.28327719, 0.86585652, 0.66163084],
         [1.80828697, 1.24887998, 0.70235812],
         [2.66044828, 1.35045788, 0.68215603],
         [1.33065745, 1.4577204 , 0.75933679]]),
  array([[1.28560483, 0.98658628, 0.67595305],
         [1.73489671, 1.482433  , 0.71539607],
         [1.29564167, 1.44918617, 0.74288636],
         [2.43989581, 1.19118473, 0.64724577]]),
  array([[1.27456576, 1.57166264, 0.854981  ],
         [1.87001532, 1.57796163, 0.66740871],
         [2.74672303, 1.29211241, 0.63669436],
         [1.35104199, 0.84856452, 0.69297247]]),
  array([[1.38296077, 0.91410661, 0.68056606],
         [1.68320947, 1.42367818, 0.6659204 ],
         [1.26965674, 1.55126723, 0.73756696],
         [2.28880844, 1.27031044, 0.66577891]])],
 [array([[1.72877886, 1.47973077, 0.68263402],
         [2.28954891, 1.47387583, 0.72014133],
         [1.25488202, 1.52890787, 0.72603781],
         [1.36624708, 1.02959695, 0.72986648]]),
  array([[1.78269554, 1.45968652, 0.65845671],
         [1.29550163, 1.56630194, 0.80255398],
         [1.33910381, 1.06375653, 0.73887124],
         [2.99602633, 1.32380946, 0.71921367]]),
  array([[1.32761929, 0.86097994, 0.61124086],
         [1.36946819, 1.64210996, 0.66995842],
         [1.29004191, 1.69784434, 1.17951575],
         [2.29966943, 1.71713578, 0.62684209]]),
  array([[1.50548041, 1.56619072, 0.64304549],
         [2.38288223, 1.6995361 , 0.62946513],
         [1.28558107, 0.78421077, 0.60182813],
         [1.22364377, 1.6643322 , 1.00434432]])]]

pd.DataFrame(centroid)

0   1   2   3
0   [[1.283277189792161, 0.8658565155306925, 0.661...   [[1.2856048285071469, 0.9865862768448912, 0.67...   [[1.274565759781191, 1.5716626415220676, 0.854...   [[1.3829607676718185, 0.9141066092756043, 0.68...
1   [[1.7287788611203834, 1.479730766338439, 0.682...   [[1.7826955386102115, 1.4596865242143404, 0.65...   [[1.3276192850743926, 0.8609799418002607, 0.61...   [[1.5054804147099767, 1.566190719572681, 0.643...


如果我只是将它们放在pd.Dataframe中,则显示如下。我试图通过此代码更改列的名称。

pd.DataFrame({'Summar':centroid[:,0],'Autumn':centroid[:,1],'Winter':centroid[:,2],'Spring':centroid[:,3]})

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-91-5cb5f6e37746> in <module>()
----> 1 pd.DataFrame({'Summar':Array[:,0],'Autumn': Array[:,1],'Winter': Array[:,2],'Spring': Array[:,3]})

TypeError: list indices must be integers or slices, not tuple

但它显示此错误...。

1 个答案:

答案 0 :(得分:0)

您的Array是包含数组的列表的列表。尝试使用

将其转换为数组
Array = np.array(Array)

然后检查Array

的形状
print (Array.shape)
# (2, 4, 4, 3)

您现在应该可以使用切片