2

I have a Numpy array as a list of lists with dimension of n by 4 (row, column). I am trying to separate the data from each individual list instance into four separate arrays each containing all the information from a single column so I can add it to a pandas data frame. From this:

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

To this:

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

Current Code:

   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
  • 2
    You say you want to create four separate arrays "so I can add it to a pandas data frame", but if all you want is df, pd.DataFrame(MasterList, columns=columns) would have worked. Why do you need the separation, and why the reshape? Commented Aug 30, 2018 at 16:50

1 Answer 1

2

As @DSM mentioned, you can do it like this:

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)

Output

   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

There is no need to reshape the array. If you want separate lists, you can try this:

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)

Output

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]

Finally you can set the values directly:

bBand = list(data[:, 0])
gBand = list(data[:, 1])
rBand = list(data[:, 2])
_class = list(data[:, 3])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.