Python 将Numpy数组拆分为每列的数组

Python 将Numpy数组拆分为每列的数组,python,pandas,numpy,split,Python,Pandas,Numpy,Split,我有一个Numpy数组作为维度为n×4(行、列)的列表列表。我试图将每个列表实例中的数据分离为四个单独的数组,每个数组包含单个列中的所有信息,以便将其添加到数据框中。 由此: [[126 188 166 1] [111 173 149 1] [ 81 119 123 2] [ 83 122 124 2] [ 84 122 124 2] [255 255 255 3] [255 255 255 3] [255 255 255 3]] 为此: bBan

我有一个Numpy数组作为维度为n×4(行、列)的列表列表。我试图将每个列表实例中的数据分离为四个单独的数组,每个数组包含单个列中的所有信息,以便将其添加到数据框中。 由此:

[[126 188 166   1]
 [111 173 149   1]
 [ 81 119 123   2]
 [ 83 122 124   2]
 [ 84 122 124   2]
 [255 255 255   3]
 [255 255 255   3]
 [255 255 255   3]]
为此:

bBand = [126,111,81,...,255]
gBand = [188,173,119,...,255]
rBand = [166,149,123,...,255]
class = [1,1,2,...,3]
当前代码:

   MasterList = np.arrray([[126, 188, 166,   1],[111, 173, 149,   1],[ 81, 119, 123,   2],[ 83, 122, 124,   2],[ 84, 122, 124,   2],[255, 255, 255,   3],[255, 255, 255,   3],[255, 255, 255,   3]])
   print(MasterList)
   columns = ["bBand","gBand","rBand","class"]
   df = pd.DataFrame(MasterList.reshape(-1, len(MasterList)),columns=columns)

正如@DSM所提到的,您可以这样做:

import numpy as np
import pandas as pd

data = np.array([[126, 188, 166, 1],
                 [111, 173, 149, 1],
                 [81, 119, 123, 2],
                 [83, 122, 124, 2],
                 [84, 122, 124, 2],
                 [255, 255, 255, 3],
                 [255, 255, 255, 3],
                 [255, 255, 255, 3]])

frame = pd.DataFrame(data=data, columns=["bBand","gBand","rBand","class"])
print(frame)
输出

   bBand  gBand  rBand  class
0    126    188    166      1
1    111    173    149      1
2     81    119    123      2
3     83    122    124      2
4     84    122    124      2
5    255    255    255      3
6    255    255    255      3
7    255    255    255      3
bBand [126 111  81  83  84 255 255 255]
gBand [188 173 119 122 122 255 255 255]
rBand [166 149 123 124 124 255 255 255]
class [1 1 2 2 2 3 3 3]
无需重新调整阵列的形状。如果需要单独的列表,可以尝试以下方法:

data = np.array([[126, 188, 166, 1],
                 [111, 173, 149, 1],
                 [81, 119, 123, 2],
                 [83, 122, 124, 2],
                 [84, 122, 124, 2],
                 [255, 255, 255, 3],
                 [255, 255, 255, 3],
                 [255, 255, 255, 3]])


for name, column in zip(["bBand","gBand","rBand","class"], data.T):
    print(name, column)
输出

   bBand  gBand  rBand  class
0    126    188    166      1
1    111    173    149      1
2     81    119    123      2
3     83    122    124      2
4     84    122    124      2
5    255    255    255      3
6    255    255    255      3
7    255    255    255      3
bBand [126 111  81  83  84 255 255 255]
gBand [188 173 119 122 122 255 255 255]
rBand [166 149 123 124 124 255 255 255]
class [1 1 2 2 2 3 3 3]
最后,您可以直接设置值:

bBand = list(data[:, 0])
gBand = list(data[:, 1])
rBand = list(data[:, 2])
_class = list(data[:, 3])

你说你想创建四个独立的数组“这样我就可以把它添加到熊猫数据框中”,但如果你只想df,
pd.DataFrame(MasterList,columns=columns)
就可以了。为什么需要分离,为什么需要重塑?