代码之家  ›  专栏  ›  技术社区  ›  C.Radford

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

  •  1
  • C.Radford  · 技术社区  · 6 年前

    我有一个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)
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Dani Mesejo    6 年前

    正如@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
    

    无需重塑阵列。如果需要单独的列表,可以尝试以下方法:

    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 [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])