Search code examples
pythonpandascsvmultidimensional-arraymulti-index

Convert 3D array into a single table to save to a csv file? And then go back to the same format?


I have a 3-dimensional array:

A = np.random.randint(0, 1000, (5, 4, 3))

Output:

array([[[101, 131, 271],
        [565, 497,  30],
        [119, 593, 630],
        [143, 926, 522]],

       [[976, 951, 237],
        [939, 864, 865],
        [257, 851, 194],
        [618, 353, 485]],

       [[264, 504, 675],
        [667, 702, 313],
        [ 97, 476, 655],
        [ 70, 174, 125]],

       [[403, 424, 594],
        [ 78, 643, 761],
        [974,  13, 171],
        [107, 681, 132]],

       [[717, 127, 800],
        [997, 813, 979],
        [942,  15, 621],
        [488, 489, 210]]])

The first value, number 5, is the index of each matrix. How can I turn this into a single table to save to a csv file, and after reading with pandas the csv file, go back to that same 3D array format?


Solution

  • You can save the file using:

    >>> df = pd.DataFrame(
                           A.reshape((-1, A.shape[-1])), 
                           index= pd.MultiIndex.from_product(
                              [range(A.shape[0]), range(A.shape[1])]
                              )
                          ).to_csv(filename)
    

    Then after loading the file, using index_col=[0,1], convert to array using:

    >>> df = pd.read_csv(filename, header=None, index_col=[0,1])
    >>> np.array(df.agg(list, 1).groupby(level=0).agg(list).tolist())
    array([[[101, 131, 271],
            [565, 497,  30],
            [119, 593, 630],
            [143, 926, 522]],
    
           [[976, 951, 237],
            [939, 864, 865],
            [257, 851, 194],
            [618, 353, 485]],
    
           [[264, 504, 675],
            [667, 702, 313],
            [ 97, 476, 655],
            [ 70, 174, 125]],
    
           [[403, 424, 594],
            [ 78, 643, 761],
            [974,  13, 171],
            [107, 681, 132]],
    
           [[717, 127, 800],
            [997, 813, 979],
            [942,  15, 621],
            [488, 489, 210]]])