Search code examples
pythonpandasnumpy4d

4D array numpy into pandas


I'd like to transform my numpy array ( shape=(27, 77, 77) ) :

   [[1., 1., 1., ..., 1., 1., 1.],
    [1., 1., 1., ..., 1., 1., 1.],
    [1., 1., 1., ..., 1., 1., 1.],
    ...,
    [1., 1., 1., ..., 2., 2., 2.],
    [1., 1., 1., ..., 2., 2., 2.],
    [1., 1., 1., ..., 1., 2., 2.]],

   ...,

   [[1., 1., 1., ..., 1., 1., 0.],
    [1., 1., 1., ..., 1., 1., 0.],
    [1., 1., 1., ..., 1., 1., 0.],
    ...,
    [1., 1., 1., ..., 1., 1., 1.],
    [1., 1., 1., ..., 1., 1., 1.],
    [1., 1., 1., ..., 1., 1., 1.]])

into a pandas dataframe with columns 'x' = index 2 (right), 'y' = index 1 (down), 'z' = index 0 ( the 27 "different" arrays) and 'v' = the values in it. df.columns=['x','y','z','v']

I'm relatively new to python, do you know how I should code this?

Thanks !


Solution

  • This does it in a primitive way.

    import numpy as np
    import pandas as pd
    
    data = np.ones( (27,77,77) )
    
    rows = []
    for i,plane in enumerate(data):
        for j,row in enumerate(plane):
            for k,col in enumerate(row):
                rows.append( [k,j,i,col] )
    
    df = pd.DataFrame( rows, columns=['x','y','z','val'])
    print(df)
    

    Output:

    C:\tmp>python x.py
             x   y   z  val
    0        0   0   0  1.0
    1        1   0   0  1.0
    2        2   0   0  1.0
    3        3   0   0  1.0
    4        4   0   0  1.0    
    ...     ..  ..  ..  ...
    160078  72  76  26  1.0
    160079  73  76  26  1.0
    160080  74  76  26  1.0
    160081  75  76  26  1.0
    160082  76  76  26  1.0
    
    [160083 rows x 4 columns]
    
    C:\tmp>