0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 65 70 77 255 66 71 77 255 67 72 78 255 66 71 77 255 65 69 76 255 65
1 166 171 175 255 171 175 178 255 175 179 181 255 181 183 185 255 184 185 186 255 183
2 63 67 70 255 72 75 78 255 86 89 91 255 101 102 104 255 110 110 112 255 119
3 51 51 59 255 45 45 53 255 47 47 55 255 52 52 60 255 57 57 65 255 64
4 75 78 87 255 72 75 84 255 70 73 82 255 68 71 80 255 68 71 80 255 67
5 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 81
6 38 61 79 255 35 58 76 255 33 56 74 255 36 58 76 255 39 60 77 255 40
7 61 83 103 255 67 89 109 255 73 94 113 255 72 93 112 255 70 91 110 255 69
8 77 94 107 255 78 93 107 255 78 93 106 255 80 94 108 255 83 96 110 255 86
9 88 100 112 255 87 99 111 255 87 98 110 255 86 97 108 255 86 96 107 255 88
10 135 143 145 255 131 139 141 255 128 136 138 255 125 133 136 255 125 133 135 255 124
11 42 56 67 255 42 56 67 255 45 57 67 255 47 57 67 255 47 56 66 255 46
所以我有一张包含像素值的表格。 但我不想要我想要的 12 行,应该只有一排一列意味着我希望每个值都应该在一列和一行中。
import glob
import pandas as pd
import numpy as np
from PIL import Image
dataSet = []
for file in glob.iglob(r'Dataset\train\building\*.png'):
img = Image.open(file)
data = np.array(img)
color_features = data.flatten()
dataSet.append(color_features)
dfData = pd.DataFrame(dataSet)
n = 10000
dataDF = dfData.iloc[:, :n]
df_Data = dfData.stack().groupby(level=0).apply(', '.join).to_frame('new')
print(df_data)
错误
TypeError: sequence item 0: expected str instance, float found
请帮忙... 谢谢
答案 0 :(得分:0)
stack() -> https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.stack.html
如果字符串:
df2 = df.stack().groupby(level=0).apply(', '.join).to_frame('new')
print (df2)
如果是整数:
df2 = df.stack().groupby(level=0)
print (df2)
您可以使用 ravel 将列方向的值展平,速度要快得多。 (如果您的数据是系列的)
https://pandas.pydata.org/docs/reference/api/pandas.Series.ravel.html
df = pd.DataFrame({'new':df.values.ravel()})