将3D Numpy数组转换为数据框

时间:2020-04-27 11:40:30

标签: python pandas numpy

我有1000张100x100像素的图像,我想将numpy数组转换为数据帧,其中numpy数组的第一个通道在前100列中,第二个通道在后100列中,依此类推。例如:

我的数组

import numpy as np

my_images=np.random.uniform(0,1,(1000,100,100,3))
my_images[0].shape
(100, 100, 3)

我想要什么:

#           channel red         chanel green        channel blue
           r1   r2 ... r100   g1   g2 ... g100   b1   b2 ... b100
image0    ...
image1    ...
...
image99   ...

我该怎么办?

谢谢

2 个答案:

答案 0 :(得分:1)

这不是最快的解决方案,但可以为您提供所需的输出。另外,每个通道有100 * 100 = 10000像素,而不是您显示的100。

import numpy as np
import pandas as pd

my_images=np.random.uniform(0,1,(1000,100,100,3))

nums = my_images.shape[0]
w = 100
h = 100
c = 3

pd.DataFrame(data= [my_images[i].flatten().reshape(w*h*c) for i in range(nums)],    # values
              index = ['img' + str(i) for i in range(nums)],    # 1st column as index
              columns= ['r' + str(i+1) for i in range(100*100)] + ['g'+str(i+1) for i in range(100*100)] + ['b'+str(i+1) for i in range(100*100)] )  # 1st row as the column names
    r1  r2  r3  r4  r5  r6  r7  r8  r9  r10     r11     r12     r13     r14     r15     r16     r17     r18     r19     r20     r21     r22     r23     r24     r25     r26     r27     r28     r29     r30     r31     r32     r33     r34     r35     r36     r37     r38     r39     r40     ...     b9961   b9962   b9963   b9964   b9965   b9966   b9967   b9968   b9969   b9970   b9971   b9972   b9973   b9974   b9975   b9976   b9977   b9978   b9979   b9980   b9981   b9982   b9983   b9984   b9985   b9986   b9987   b9988   b9989   b9990   b9991   b9992   b9993   b9994   b9995   b9996   b9997   b9998   b9999   b10000
img0    0.486932    0.358114    0.378462    0.962743    0.967968    0.858799    0.355605    0.557928    0.180849    0.979154    0.058116    0.287494    0.479971    0.263553    0.881374    0.875206    0.352662    0.038444    0.120535    0.000326    0.978237    0.715863    0.350651    0.136343    0.633519    0.971797    0.194671    0.981690    0.301551    0.738752    0.910106    0.723529    0.567343    0.583138    0.925730    0.885722    0.417114    0.337083    0.140824    0.889483    ...     0.649809    0.513012    0.653982    0.177253    0.188145    0.494424    0.455103    0.024664    0.012357    0.956512    0.873941    0.977735    0.374367    0.101984    0.989629    0.785725    0.839505    0.944744    0.748421    0.781885    0.233788    0.829904    0.907176    0.157434    0.382110    0.460945    0.732198    0.130351    0.607728    0.200979    0.496550    0.272893    0.490625    0.529867    0.975578    0.688915    0.219779    0.253977    0.351022    0.738245
img1    0.045102    0.028449    0.777591    0.575000    0.868562    0.192366    0.925344    0.934355    0.476159    0.336614    0.058352    0.706517    0.700616    0.697568    0.931899    0.298687    0.325758    0.844186    0.975191    0.815392    0.819782    0.942618    0.119667    0.883005    0.125012    0.314823    0.161448    0.790827    0.534900    0.844786    0.975306    0.347407    0.661538    0.098459    0.867604    0.208455    0.729929    0.063040    0.798002    0.976301    ...     0.123317    0.469142    0.022312    0.238848    0.579594    0.233403    0.111962    0.180687    0.929240    0.283651    0.125432    0.484908    0.734587    0.788714    0.675700    0.663587    0.596222    0.216500    0.783715    0.330661    0.703489    0.780786    0.111286    0.804458    0.691808    0.709701    0.874952    0.278902    0.499596    0.863403    0.554768    0.432910    0.189384    0.196691    0.024404    0.492914    0.041698    0.782992    0.330925    0.681564
img2    0.895364    0.736759    0.034967    0.331774    0.179954    0.064767    0.431156    0.345568    0.516664    0.724786    0.283170    0.077229    0.382801    0.734160    0.964772    0.630064    0.304190    0.014286    0.466788    0.624111    0.593372    0.773352    0.018929    0.515174    0.665516    0.614239    0.184120    0.940892    0.520044    0.432633    0.465579    0.458798    0.623848    0.452827    0.442585    0.722681    0.631540    0.109865    0.263037    0.432471    ...     0.344212    0.161572    0.934545    0.761583    0.048125    0.004187    0.532132    0.275795    0.730705    0.714795    0.220308    0.624861    0.518679    0.838271    0.081648    0.528557    0.316329    0.301952    0.647050    0.141154    0.528354    0.733596    0.213516    0.876608    0.399335    0.785443    0.908612    0.977647    0.869134    0.438461    0.477976    0.191026    0.008590    0.734226    0.331556    0.277559    0.358993    0.429474    0.209247    0.741306
img3    0.042249    0.215090    0.609098    0.136100    0.480906    0.270167    0.275241    0.418473    0.119309    0.075258    0.476748    0.151002    0.013587    0.695470    0.062000    0.183760    0.685605    0.224058    0.354772    0.558781    0.801372    0.136810    0.585566    0.468125    0.754648    0.205976    0.204213    0.055228    0.715287    0.069664    0.682018    0.219368    0.629850    0.469663    0.971606    0.445856    0.130807    0.859502    0.395481    0.101970    ...     0.642661    0.627955    0.962984    0.436121    0.226712    0.343389    0.158353    0.032410    0.197190    0.487362    0.649477    0.227321    0.106230    0.635838    0.822605    0.181413    0.134469    0.536877    0.229766    0.014072    0.076003    0.715866    0.962098    0.693023    0.536637    0.628003    0.953686    0.471928    0.969996    0.777448    0.709374    0.043167    0.590571    0.264927    0.034542    0.947914    0.723615    0.555356    0.292524    0.860090
img4    0.342812    0.984872    0.595325    0.560725    0.322496    0.106175    0.623969    0.517042    0.202604    0.899584    0.357242    0.211253    0.336435    0.215450    0.651622    0.296562    0.932716    0.904923    0.832153    0.297773    0.079881    0.898914    0.862357    0.219088    0.874832    0.945539    0.854319    0.141670    0.450833    0.324066    0.157684    0.506456    0.650278    0.104939    0.918043    0.615409    0.487017    0.914478    0.525752    0.532596    ...     0.052621    0.648331    0.289361    0.357715    0.054250    0.827826    0.628299    0.704747    0.777197    0.948778    0.542223    0.111109    0.034345    0.818893    0.180993    0.809252    0.258662    0.440389    0.973193    0.629529    0.722974    0.164910    0.376697    0.417736    0.402239    0.203207    0.352650    0.082466    0.723988    0.192275    0.905494    0.271835    0.148498    0.357596    0.775139    0.631658    0.683252    0.444594    0.562559    0.270776
...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...
img995  0.838085    0.397261    0.846695    0.933139    0.620105    0.532176    0.797445    0.527347    0.239898    0.905126    0.958340    0.044355    0.066412    0.744415    0.909536    0.704189    0.798124    0.971813    0.284309    0.199504    0.707818    0.678898    0.184314    0.265278    0.286230    0.252277    0.698063    0.052999    0.163469    0.568355    0.949519    0.673165    0.845369    0.939780    0.536132    0.943460    0.154108    0.642619    0.215590    0.661864    ...     0.274670    0.237090    0.793010    0.993295    0.594697    0.767856    0.837942    0.237594    0.519923    0.396707    0.094800    0.460319    0.677104    0.054613    0.804510    0.874100    0.120772    0.383306    0.703549    0.015940    0.382384    0.117743    0.412058    0.374512    0.839501    0.533861    0.858508    0.014900    0.127974    0.784746    0.480787    0.754446    0.730582    0.213735    0.493690    0.590700    0.477921    0.767106    0.460363    0.898690
img996  0.171881    0.666083    0.567388    0.534970    0.110068    0.950682    0.738784    0.624271    0.147806    0.285420    0.009684    0.403910    0.345770    0.232941    0.261416    0.620245    0.504229    0.436067    0.822144    0.746396    0.853005    0.843115    0.587636    0.114130    0.502100    0.780388    0.819707    0.332244    0.592359    0.000251    0.156495    0.764803    0.509039    0.220428    0.829552    0.390561    0.074693    0.815926    0.113959    0.118536    ...     0.685953    0.517420    0.152370    0.961478    0.500367    0.779224    0.331803    0.628549    0.399974    0.404072    0.549934    0.142945    0.061841    0.713861    0.842240    0.740575    0.520969    0.094217    0.480275    0.609732    0.324852    0.355591    0.059871    0.571136    0.275547    0.463229    0.573066    0.628505    0.131310    0.916291    0.501336    0.734825    0.550189    0.852415    0.036260    0.163183    0.379861    0.151860    0.587352    0.558592
img997  0.260229    0.027160    0.024845    0.405843    0.153609    0.948280    0.833786    0.443430    0.733487    0.469510    0.492071    0.093200    0.748563    0.443063    0.103739    0.262570    0.326819    0.038774    0.654335    0.505306    0.025019    0.153099    0.495584    0.825881    0.127388    0.579630    0.622424    0.203833    0.264736    0.339902    0.958501    0.130448    0.809757    0.480960    0.456775    0.888074    0.660926    0.866277    0.489128    0.986085    ...     0.629622    0.532311    0.198096    0.512762    0.025338    0.179305    0.564835    0.844087    0.550143    0.044381    0.993802    0.231412    0.231025    0.069339    0.867986    0.877767    0.276351    0.438310    0.931497    0.382128    0.727942    0.755447    0.367368    0.672704    0.846903    0.960334    0.227707    0.921844    0.224192    0.481621    0.273852    0.521445    0.387418    0.594588    0.634387    0.752935    0.966900    0.286578    0.848091    0.761919
img998  0.137022    0.363908    0.840758    0.347840    0.119981    0.367948    0.039828    0.731761    0.959635    0.133150    0.344888    0.328250    0.969160    0.089202    0.044397    0.727128    0.707633    0.459997    0.049261    0.222913    0.586730    0.410247    0.689743    0.954076    0.067321    0.008652    0.250084    0.997298    0.653954    0.036369    0.209113    0.606945    0.375438    0.423460    0.874191    0.960310    0.002205    0.638605    0.722848    0.280049    ...     0.956299    0.122053    0.198945    0.632034    0.103117    0.480278    0.658445    0.703479    0.842846    0.589640    0.150563    0.580881    0.388750    0.084818    0.746425    0.590966    0.751141    0.515122    0.370861    0.233196    0.943156    0.965516    0.463034    0.055458    0.436112    0.722325    0.051160    0.443859    0.505781    0.547766    0.798339    0.890893    0.146561    0.803766    0.721480    0.386739    0.883938    0.182097    0.549544    0.276338
img999  0.744549    0.724202    0.889157    0.177200    0.120874    0.013192    0.432094    0.973115    0.825215    0.568912    0.608280    0.439619    0.067831    0.454721    0.987532    0.509805    0.760051    0.581479    0.331267    0.600631    0.780780    0.117506    0.583506    0.593765    0.976454    0.143185    0.344449    0.330625    0.188884    0.919397    0.623545    0.686960    0.423576    0.173410    0.325512    0.806801    0.492669    0.717346    0.988060    0.079150    ...     0.468972    0.621811    0.315449    0.698777    0.073734    0.252283    0.990783    0.652986    0.778134    0.263407    0.142558    0.558776    0.765833    0.641718    0.934577    0.545056    0.974763    0.392286    0.026937    0.546414    0.040816    0.939266    0.325401    0.760387    0.131496    0.495096    0.711066    0.192676    0.600273    0.266118    0.258884    0.771196    0.902020    0.572795    0.983741    0.886390    0.623550    0.236895    0.498307    0.757591

1000 rows × 30000 columns

答案 1 :(得分:1)

根据您要“展平”每种颜色的顺序,可以执行以下操作:

import numpy as np
import pandas as pd

N = 1000
d = 100
ims = np.random.uniform(0, 1, (N, d, d, 3))

cols = [f'r{i}' for i in range(d**2)] + [f'g{i}' for i in range(d**2)] + [f'b{i}' for i in range(d**2)]

df = pd.DataFrame(data = np.swapaxes(ims, 1,3).reshape(N, -1),
                  columns = cols,
                  index = [f'image{i}' for i in range(N)])
df
               r0        r1        r2  ...     b9997     b9998     b9999
image0    0.754808  0.830338  0.352381  ...  0.870754  0.654643  0.579047
image1    0.387347  0.197181  0.253846  ...  0.091151  0.900780  0.555289
image2    0.743756  0.465971  0.269931  ...  0.946937  0.117723  0.497232
image3    0.899940  0.891160  0.701490  ...  0.194214  0.819856  0.485955
image4    0.593754  0.161847  0.342187  ...  0.481527  0.213749  0.978097
          ...       ...       ...  ...       ...       ...       ...
image995  0.693697  0.297598  0.258053  ...  0.971533  0.430261  0.222101
image996  0.818448  0.658862  0.339770  ...  0.613745  0.776705  0.532044
image997  0.376489  0.180131  0.703525  ...  0.933989  0.326170  0.833145
image998  0.757319  0.652464  0.125789  ...  0.675353  0.860467  0.394837
image999  0.021546  0.670330  0.024574  ...  0.617004  0.156939  0.415484

[1000 rows x 30000 columns]