我有一个Numpy数组作为列表的列表,其中n乘4(行,列)。我试图将每个单独的列表实例中的数据分成四个单独的数组,每个数组包含来自同一列的所有信息,因此我可以将其添加到pandas数据框中。 从这个:
[[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)
答案 0 :(得分:2)
正如@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])