Zizi96
Zizi96

Reputation: 519

How do I convert a 3-dimensional NumPy array into a dataframe?

How do I convert a 3-dimensional NumPy array [sample_index, channel, data] into a dataframe with each sample as a row and the channels as a column?

The numpy arrays look like this:

import numpy as np
np.random.uniform(-25, 100, size=(6400,3,100*100))

Upvotes: 1

Views: 105

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Let's try:

samples, size = 6, 2*2

data = np.arange(samples*size *3).reshape(samples, 3, size)

pd.DataFrame(data.swapaxes(1,2).reshape(-1,3),
             np.repeat(np.arange(samples), size)
            )

Output, here, each index represents a sample:

    0   1   2
0   0   4   8
0   1   5   9
0   2   6  10
0   3   7  11
1  12  16  20
1  13  17  21
1  14  18  22
1  15  19  23
2  24  28  32
2  25  29  33
2  26  30  34
2  27  31  35
3  36  40  44
3  37  41  45
3  38  42  46
3  39  43  47
4  48  52  56
4  49  53  57
4  50  54  58
4  51  55  59
5  60  64  68
5  61  65  69
5  62  66  70
5  63  67  71

Upvotes: 1

Related Questions